android - Why do I not need to use Adapter.notifyDataSetChanged()? -
the contactslist
empty until readcontacts()
method executed, in other words, when contactsview.setadapter(adapter)
executed, contactslist
empty, why code still can show contacts' info correctly?
public class mainactivity extends appcompatactivity { listview contactsview; arrayadapter<string> adapter; list<string> contactslist = new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); contactsview = (listview) findviewbyid(r.id.contacts_list); adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, contactslist); contactsview.setadapter(adapter); readcontacts(); } private void readcontacts() { cursor cursor = null; try { cursor = getcontentresolver().query( contactscontract.commondatakinds.phone.content_uri, null, null, null, null); while (cursor.movetonext()) { string displayname = cursor.getstring(cursor.getcolumnindex( contactscontract.commondatakinds.phone.display_name )); string number = cursor.getstring(cursor.getcolumnindex( contactscontract.commondatakinds.phone.number )); contactslist.add(displayname + "\n" + number); } } catch (exception e) { e.printstacktrace(); } { if (cursor != null) { cursor.close(); } } } }
but if add this, have call notifydatasetchanged()
:
add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { contactslist.add("blabla"); adapter.notifydatasetchanged(); } });
add
button. android call method automatically, why when remove adapter.notifydatasetchanged();
ui couldn't refresh?
the point entering data in order i.e. when pushed item in list goes way down , u didn't enter @ 4th , 5th random index don't have call notifydatasetchanged()
definition says : that data has been changed or view reflecting data set should refresh make new data visible on list, in case data going out of scope of visible list i.e. number of child being shown in list it(the listview) calls next views after particular last shown index's value (item) in listview.!
hope made bit clear you...!
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ answer after edited question, after searching , having own thoughts , understandings in regards :
in first case there no change needed made view.. right.? if point clear come on second. in first, fetched data db , added item in list populate after setting adapter.!! adapter wasn't set until whole method executes , completes list..! ok.
but in second scenario changing view (pretty view in android) u manipulating view adding item on populated view (which has set-ted adapter), time u need tell view hey , have added item in refresh update list (i.e. display).
Comments
Post a Comment