Android ListView not refreshing after notifyDataSetChanged with data as Map -
i used list adapter map values data. when use adapter.notifydatasetchanged();
data in list not updating. if replace map arraylist working fine. following adapter code.
public class categoryadapter extends baseadapter { arraylist<category> list = new arraylist<category>(); context context; public categoryadapter(context context, map<string, category> categories) { this.context = context; list.clear(); list.addall(categories.values()); } @override public int getcount() { return list.size(); } @override public view getview(int position, view convertview, viewgroup parent) { final viewhandler handler; layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { convertview = inflater.inflate(r.layout.category_list_item, null); handler = new viewhandler(); handler.name = (textview) convertview.findviewbyid(r.id.name); handler.count = (textview) convertview.findviewbyid(r.id.count); convertview.settag(handler); } else { handler = (viewhandler) convertview.gettag(); } category category = list.get(position); handler.name.settext(category.getmenuname()); handler.count.settext(category.getcount() + ""); if (category.getcount() <= 0) { handler.count.setvisibility(view.invisible); } else { handler.count.setvisibility(view.visible); } return convertview; } }
and activity code
private void loadcategories() { sampledb = openorcreatedatabase(appconstants.db_name, mode_private, null); cursor menucursor = sampledb.rawquery("select * menu", null); categories.clear(); while (menucursor.movetonext()) { string menu = menucursor.getstring(menucursor .getcolumnindex("name")); string id = menucursor.getstring(menucursor.getcolumnindex("id")); category category = new category(menu, id); categories.put(id, category); } menucursor.close(); categoryadapter.notifydatasetchanged(); }
in oncreate()
method have declared adapter follows
categoryadapter = new categoryadapter(context, categories); categorylist.setadapter(categoryadapter); loadcategories();
every time click on refresh button calls loadcategories();
method. in same code if replace map arraylist working fine.
now question why list not refreshing map values. please give me clarification regarding this.
thanks in advance.
you must add method categoryadapter
change instance's list, so
public class categoryadapter extends baseadapter { arraylist<category> list = new arraylist<category>(); context context; public categoryadapter(context context, map<string, category> categories) { this.context = context; list.clear(); list.addall(categories.values()); } @override public int getcount() { return list.size(); } //add method change list public void additems(map<string, category> categories){ list.clear(); list.addall(categories.values()); } @override public view getview(int position, view convertview, viewgroup parent) { final viewhandler handler; layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); if (convertview == null) { convertview = inflater.inflate(r.layout.category_list_item, null); handler = new viewhandler(); handler.name = (textview) convertview.findviewbyid(r.id.name); handler.count = (textview) convertview.findviewbyid(r.id.count); convertview.settag(handler); } else { handler = (viewhandler) convertview.gettag(); } category category = list.get(position); handler.name.settext(category.getmenuname()); handler.count.settext(category.getcount() + ""); if (category.getcount() <= 0) { handler.count.setvisibility(view.invisible); } else { handler.count.setvisibility(view.visible); } return convertview; } }
and change loadcategories (note call additems() before notifydatasetchanged()
private void loadcategories() { sampledb = openorcreatedatabase(appconstants.db_name, mode_private, null); cursor menucursor = sampledb.rawquery("select * menu", null); categories.clear(); while (menucursor.movetonext()) { string menu = menucursor.getstring(menucursor .getcolumnindex("name")); string id = menucursor.getstring(menucursor.getcolumnindex("id")); category category = new category(menu, id); categories.put(id, category); } menucursor.close(); //add call additems update list of categoryadapter instance categoryadapter.additems(categories); categoryadapter.notifydatasetchanged(); }
Comments
Post a Comment