Dynamically add data to ListView in android

ⅰ亾dé卋堺 提交于 2019-12-18 09:07:36

问题


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!!!
         * 
        */
    }
}

回答1:


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();



回答2:


Have a look at ExpandableListView

Another tutorial



来源:https://stackoverflow.com/questions/14232258/dynamically-add-data-to-listview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!