Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically adding custom row to listview

By referring this, I created following:

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/addBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="addItems"
        android:text="Add New Item" />

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
    ListView list;   
    ArrayList<String> listItems = new ArrayList<String>();    
    ArrayAdapter<String> adapter;   
    int clickCounter = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (ListView) findViewById(R.id.list);
        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, listItems);
        list.setAdapter(adapter);

        list.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                String item = list.getItemAtPosition(position).toString();
                Log.i("MainActivity", "Selected = " + item);
            }
        });
    }

    public void addItems(View v) {
        listItems.add("Clicked : " + clickCounter++);
        adapter.notifyDataSetChanged();
    }
}

And it's working perfectly. But as per requirements, my each listview row won't just be a single string. Instead, it'll be collection of views consisting of imageview and textviews stored in row.xml.

Now my queries are:

  • What will replace adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, listItems); ? Will it be adapter = new ArrayAdapter<String>(this,R.layout.row, listItems); ??

  • How do I refer to imageview and textviews of each row? How do I set and get data from them? How do I recognize their click events?

  • Is use of Adapter must? or can I get away with it?

Any help appreciated.

like image 591
GAMA Avatar asked Jun 06 '13 08:06

GAMA


2 Answers

But as per requirements, my each listview row won't just be a single string. Instead, it'll be collection of views consisting of imageview and textviews stored in row.xml.

=> The ListView you are displaying is using normal adapter. If you want your item contains multiple views like Imageview, Textview or any view, then you have to define Custom adapter class by extending either BaseAdapter or ArrayAdapter.

What will replace adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, listItems); ? Will it be adapter = new ArrayAdapter(this,R.layout.row, listItems); ??

=> Here ArrayAdapter will not work because your row xml layout may contains different views like ImageView, TextView, Button or any other widget. So I would suggest you to define your own adapter class where you have to override getView() method.

How do I refer to imageview and textviews of each row? How do I set and get data from them? How do I recognize their click events?

=> As I said above, once you define custom adapter class, you will have to override getView() method where you can find any views of your row xml layout file, reference it and set/display whatever data you want.

Is use of Adapter must? or can I get away with it?

=> Yes its must, without adapter you won't be able to display in data-binded widgets like GridView, ListView, Spinner, Gallery, etc.

Example for defining custom adapter:

  1. My talk on ListView
  2. http://www.vogella.com/articles/AndroidListView/article.html
like image 180
Paresh Mayani Avatar answered Nov 18 '22 16:11

Paresh Mayani


These are the basic steps:

  • Create a custom layout for your row (maybe with an ImageView and TextView in it). You used android.R.layout.simple_list_item_1 in your example which if you look into the Android source is just a layout with a single TextView.
  • Create a class that extends BaseAdapter. This will be your list adapter. You can pass the data to your adapter through the constructor or a method. Create a field where you will store the data.

Now to answer your questions:

  • How do I refer to imageview and textviews of each row?
  • How do I set and get data from them?
  • How do I recognize their click events?

When you extend BaseAdapter you will implement the method public View getView (int position, View convertView, ViewGroup parent). In this method you have to inflate your custom row layout to create the view. Then find the ImageView and TextView using the findViewById method. When you have the ImageView and TextView you call setText or setImageSource to set your data and setOnClickListener for the click events.

like image 33
jfs Avatar answered Nov 18 '22 16:11

jfs