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.
Solution 1:
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.
Solution 2:
instead of
listItems.add("New Item");
adapter.notifyDataSetChanged();
you can directly call
adapter.add("New Item");
Solution 3:
First, you have to add a ListView, an EditText and a button into your activity_main.xml.
Now, in your ActivityMain:
private EditText editTxt;
private Button btn;
private ListView list;
private ArrayAdapter<String> adapter;
private ArrayList<String> arrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTxt = (EditText) findViewById(R.id.editText);
btn = (Button) findViewById(R.id.button);
list = (ListView) findViewById(R.id.listView);
arrayList = new ArrayList<String>();
// Adapter: You need three parameters 'the context, id of the layout (it will be where the data is shown),
// and the array that contains the data
adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);
// Here, you set the data in your ListView
list.setAdapter(adapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// this line adds the data of your EditText and puts in your array
arrayList.add(editTxt.getText().toString());
// next thing you have to do is check if your adapter has changed
adapter.notifyDataSetChanged();
}
});
}
This works for me, I hope I helped you