android - Failing to update ListView even after calling notifyDataSetChanged() -


the code simple, wanted see how update listview entirely new data. storing values in array , passing arrayadapter. when clicked on listview item, want clear exiting listview , add new set of values , display new items in list view.

i can see array getting updated new values, these values not displayed after calling notifydatasetchanged(). please take @ code , let me know doing wrong here. thank in advance.

public class mainactivity extends activity {    public listview mlistview;    string[] stringarray;    string[] stringarray1;    string[] stringarray2;    string[] stringarray3;    string[] stringarray4;    static int track=1;    public arrayadapter<string> adapter;    public arraylist<string[]> alist;    static iterator<string[]> aitr;  @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      mlistview=(listview) findviewbyid(r.id.listview1);      //stringarray= new string[4];     stringarray1 = new string[]  {"bright mode1", "normal mode1","silly mode1", "dupe mode1","trail mode1"};     stringarray2 = new string[] { "bright mode2", "normal mode2","silly mode2", "dupe mode2","trail mode2"};     stringarray3 = new string[] { "bright mode3", "normal mode3","silly mode3", "dupe mode3","trail mode3"};     stringarray4 = new string[] { "bright mode4", "normal mode4","silly mode4", "dupe mode4","trail mode4"};     alist=new arraylist<string[]>();     alist.add(stringarray2);     alist.add(stringarray3);     alist.add(stringarray4);     aitr=alist.iterator();      stringarray=stringarray1;     adapter = new arrayadapter<string>(this,             android.r.layout.simple_list_item_1, android.r.id.text1, stringarray);     mlistview.setadapter(adapter); }  public void updatearray(){     if(aitr.hasnext()){         mlistview.clearchoices();         stringarray=aitr.next();     } }  @override protected void onstart(){     super.onstart();     mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {             int = view.getid();              log.e("devsample", "the is: " + + ", " + position + ", " + id);             updatearray();             adapter.notifydatasetchanged() ;         }     }); } 

}

in updatearray() method assigning new array stringarray adapter still keeps reference old stringarray. add these 2 lines @ end of updatearray() update adapter:

adapter.clear(); adapter.addall(stringarray); // or if you're targeting android < 11 call: adapter.add() in loop 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -