Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add data to ListView in android

I've a listview which already contain a list of data.

What I'm trying to achieve is when I click one of the ListItem, I want to add another bunch of dataset just below the clicked item.

protected void onListItemClick(ListView l, View v, int position, long id) {
    if (position == 0) {
        /* 
         * 
         * want to add another bunch of data just below postion 0!!!
         * 
        */
    }
}
like image 267
Swan Avatar asked Dec 03 '25 17:12

Swan


1 Answers

You can insert the data in the adapter that you use with list view and then call the notifyDataSetChanged() on the adapter to update the list view. You should use an ArrayAdapter (or its subclass) to be able to dynamically add objects to the list view.

((ArrayAdapter)listView.getAdapter()).insert(object, index);
((ArrayAdapter)listView.getAdapter()).notifyDataSetChanged();
like image 198
Sapan Diwakar Avatar answered Dec 06 '25 08:12

Sapan Diwakar