Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom ListView Android

I have problem with my custom ListView.

SimpleAdapter adapter = new SimpleAdapter(this,
  this.dh.selectAll(),
  R.layout.custom_row_view,
  new String[] {"Icon","Chance","TeamID"},
  new int[] {R.id.text1,R.id.text2, R.id.text3});
  setListAdapter(adapter);

This is my simple adapter. I want put into them icons, can I do that?

like image 237
Dawid Sajdak Avatar asked Jun 10 '11 11:06

Dawid Sajdak


People also ask

What do you mean by custom list view in android?

Android Custom ListView (Adding Images, sub-title) After creating simple ListView, android also provides facilities to customize our ListView.

How do I create a custom list view?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. In the above activity_main.

What is custom Adapter in android?

What is an Adapter in Android. An Adapter object acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

Is Android ListView deprecated?

Conclusion. While the ListView is still a very capable view, for new projects, I'll strongly advise you use RecyclerView, and consider the ListView as deprecated. I can't think of any situation where the ListView is better than the RecyclerView, even if you implement your ListView with the ViewHolder pattern.


1 Answers

yes you can ...

MyList.java:

package com.TestActivity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;


public class MyList extends ListActivity {

    final static ArrayList<HashMap<String, ?>> data = new ArrayList<HashMap<String, ?>>();

    static{
        HashMap<String, Object> row  = new HashMap<String, Object>();
        row.put("Icon", R.drawable.flags_albania);
        row.put("Chance", "65%");
        row.put("TeamID", "Albania");
        data.add(row);
        row  = new HashMap<String, Object>();
        row.put("Icon", R.drawable.flags_rpa);
        row.put("Chance", "55%");
        row.put("TeamID", "RPA");
        data.add(row);
        row  = new HashMap<String, Object>();
        row.put("Icon", R.drawable.flags_polska);
        row.put("Chance", "100%");
        row.put("TeamID", "Polska :)");
        data.add(row);
    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SimpleAdapter adapter = new SimpleAdapter(this,
                data,
                  R.layout.row,
                  new String[] {"Icon","Chance","TeamID"},
                  new int[] { R.id.imageView1, R.id.textView1,R.id.textView2});
       setListAdapter(adapter);

    }
}

res/layout/row.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/icon" android:id="@+id/imageView1"/>
    <TextView android:text="TextView" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
    <TextView android:text="TextView" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</LinearLayout>

put pngs with flags to res/drawable-hdpi/

flags_albania.png flags_rpa.png flags_polska.png

and you will get smthing like this:

enter image description here

like image 90
Selvin Avatar answered Oct 01 '22 18:10

Selvin