listview - android how to set notifySetDataChanged() for an ArrayList -
i'm trying refresh listview
contents when go activity activity. refresh contents when go further main app screen , come back.
this activity set listview
, it's adapter:
public static final string id = "id"; public static final string iidd = "iidd"; private static final string desc = "description"; private appcompatdelegate delegate; cursor c; listview listfood; simpleadapter myadapter; arraylist<map<string, string>> names = new arraylist<map<string, string>>(); integer i_d; integer iddd; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activityfood_main); listfood = (listview) findviewbyid(r.id.listviewf); listfood.setonitemclicklistener(viewfoodlistener); // database read databaseconnectorfood db = new databaseconnectorfood(this); db.open(); c = db.getfooddata(); while(c.movetonext()) { map<string, string> data = new hashmap<string, string>(2); data.put("description", c.getstring(c.getcolumnindex("description"))); data.put("serving_size", c.getstring(c.getcolumnindex("serving_size"))); names.add(data); } db.close(); myadapter = new simpleadapter(this, names, r.layout.list_food, new string[] {"description", "serving_size"}, new int[] {r.id.viewfoodnotesone, r.id.viewfoodnotestwo}); listfood.setadapter(myadapter); }
i tried add under onresume()
doesn't work
@override protected void onresume() { super.onresume(); myadapter.notifydatasetchanged(); }
edit: followed suggestion answers below doing follow, no results:
@override protected void onresume() { super.onresume(); databaseconnectorfood db = new databaseconnectorfood(this); db.open(); c = db.getfooddata(); while(c.movetonext()) { map<string, string> data = new hashmap<string, string>(2); data.put("description", c.getstring(c.getcolumnindex("description"))); data.put("serving_size", c.getstring(c.getcolumnindex("serving_size"))); names.add(data); } db.close(); myadapter = new simpleadapter(this, names, r.layout.list_food, new string[] {"description", "serving_size"}, new int[] {r.id.viewfoodnotesone, r.id.viewfoodnotestwo}); // set adapter simplecursoradapter listfood.setadapter(myadapter); myadapter.notifydatasetchanged(); }
solved: added names.clear();
code on here, above everything, clear listview
first @rami suggested, , works! thanks
1) refresh list, need clear data (names
) reload again database.
2) onresume()
method called. better performance, suggest use onactivityresult()
instead, while update data childs activities.
@override protected void onactivityresult(int requestcode, int resultcode, intent data) { // refresh list here. }
in case, don't forget use startactivityforresult()
instead of startactivity()
.
Comments
Post a Comment