android - Not understanding whether to use notifyDataSetChanged() when elements in listview are updated? -
i have android application uses listview adapter of custom objects.
oncreate of activity if list of objects available populate adapter previous objects , make async call fetch new objects , populate adapter.
while populating adapter again old list of elements removed , new added. have not used notifydatasetchanged().
private customadapter listadapter; private gridview gridview; // oncreateview of fragment if find old list in singleton populate list , shows on screen function oncreateview() { ... ... listadapter = new customadapter(getactivity(), countersingleton.getinstance(getactivity()) .getobjects()); gridview.setadapter(listadapter); .... ... new asnyctask().execute(); } class asnyctask { // fetch new list , replace old list shown list<objects> newlist = fetchnewlist(); listadapter = new customadapter( getactivity(), newlist); gridview.setadapter(listadapter); }
few questions here:
do need use notifydatasetchanged() in asnyctask class after populate adapter creating new instance of it.
will old list removed , replaced new list per above code.
do need use notifydatasetchanged() in asnyctask class after populate adapter creating new instance of it.
no need call notifydatasetchanged()
because calling setadapter
method listview new data-source
will old list removed , replaced new list per above code.
yes listview populate new data.
note: instead of calling setadapter
method again-2 showing new data. create method inside adapter update current adapter new data.like:
public void addnewitems(list<objects> itemlist){ 1. clear data current adapter example if using arraylist arraylist_object.clearall(); 2. add itemlist in arraylist_object arraylist_object.addall(itemlist); 3. call this.notifydatasetchanged() }
now call addnewitems
update current apdater data asnyctask
using same object created first time when listview populated :
listadapter.addnewitems(newlist)