Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you dynamically add elements to a ListView on Android?

Can anyone explain or suggest a tutorial to dynamically create a ListView in android?

Here are my requirements:

  • I should be able to dynamically add new elements by pressing a button.
  • Should be simple enough to understand (possibly without any performance improvements or convertView, for instance)

I know there are quite a few questions on this topic, but I couldn't find any that answer my question.

like image 244
user555217 Avatar asked Dec 27 '10 18:12

user555217


2 Answers

Create an XML layout first in your project's res/layout/main.xml folder:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout     xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="fill_parent"     android:layout_height="fill_parent" >     <Button         android:id="@+id/addBtn"         android:text="Add New Item"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:onClick="addItems"/>     <ListView         android:id="@android:id/list"         android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:drawSelectorOnTop="false"     /> </LinearLayout> 

This is a simple layout with a button on the top and a list view on the bottom. Note that the ListView has the id @android:id/list which defines the default ListView a ListActivity can use.

public class ListViewDemo extends ListActivity {     //LIST OF ARRAY STRINGS WHICH WILL SERVE AS LIST ITEMS     ArrayList<String> listItems=new ArrayList<String>();      //DEFINING A STRING ADAPTER WHICH WILL HANDLE THE DATA OF THE LISTVIEW     ArrayAdapter<String> adapter;      //RECORDING HOW MANY TIMES THE BUTTON HAS BEEN CLICKED     int clickCounter=0;      @Override     public void onCreate(Bundle icicle) {         super.onCreate(icicle);         setContentView(R.layout.main);         adapter=new ArrayAdapter<String>(this,             android.R.layout.simple_list_item_1,             listItems);         setListAdapter(adapter);     }      //METHOD WHICH WILL HANDLE DYNAMIC INSERTION     public void addItems(View v) {         listItems.add("Clicked : "+clickCounter++);         adapter.notifyDataSetChanged();     } } 

android.R.layout.simple_list_item_1 is the default list item layout supplied by Android, and you can use this stock layout for non-complex things.

listItems is a List which holds the data shown in the ListView. All the insertion and removal should be done on listItems; the changes in listItems should be reflected in the view. That's handled by ArrayAdapter<String> adapter, which should be notified using:

adapter.notifyDataSetChanged();

An Adapter is instantiated with 3 parameters: the context, which could be your activity/listactivity; the layout of your individual list item; and lastly, the list, which is the actual data to be displayed in the list.

like image 54
Shardul Avatar answered Oct 20 '22 18:10

Shardul


instead of

listItems.add("New Item"); adapter.notifyDataSetChanged(); 

you can directly call

adapter.add("New Item"); 
like image 23
venkat530 Avatar answered Oct 20 '22 16:10

venkat530