android - How do I get my CustomListAdapter to update on notifyDatasetChange? -
this first time building listview custom layout, in case have missed obvious please point out.
the problem having cannot listview update new information after oncreate(); has been used. list static.
i trying create custom listview adapter looks such:
public class mainlistcustombaseadapter extends baseadapter { static arraylist<listitems> datasomething; static context cont; public mainlistcustombaseadapter (arraylist<listitems> data, context c){ datasomething = data; cont = c; } public int getcount() { // todo auto-generated method stub return datasomething.size(); } public object getitem(int position) { // todo auto-generated method stub return datasomething.get(position); } public long getitemid(int position) { // todo auto-generated method stub return position; } public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub view v = convertview; if (v == null) { layoutinflater vi = (layoutinflater)cont.getsystemservice(context.layout_inflater_service); v = vi.inflate(r.layout.mainlistlayout, null); } imageview image = (imageview) v.findviewbyid(r.id.listimage); textview titleview = (textview)v.findviewbyid(r.id.title); textview detailitemview = (textview)v.findviewbyid(r.id.detailitem); listitems msg = datasomething.get(position); image.setimageresource(msg.icon); titleview.settext(msg.title); detailitemview.settext("itemdetails: "+msg.itemdetails); return v; } public void updateresults(arraylist<mainlistcustombaseadapter> results){ notifydatasetchanged(); } }
my oncreate looks this:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); recipelist = (listview) findviewbyid(r.id.mainlistview); shoppingitems = new arraylist<listitems>(); recipelist.setadapter(new mainlistcustombaseadapter(shoppingitems, this)); listitems detail; detail = new listitems(); detail.seticon(r.drawable.food); detail.setname("food stuff"); detail.setitemdetails("itemdetailscomp"); shoppingitems.add(detail); }
and listitem looks this:
public class listitems { public int icon ; public string title; public string itemdetails; public string getname() { return title; } public void setname(string from) { this.title = from; } public string getitemdetails() { return itemdetails; } public void setitemdetails(string itemdetailscomp) { this.itemdetails = itemdetailscomp; } public int geticon() { return icon; } public void seticon(int icon) { this.icon = icon; } }
how listview update dynamically? maybe setinvalidatedviews() or notifydatasetchanged()?
any appreciated.
use below
mainlistcustombaseadapter adapter = new mainlistcustombaseadapter(shoppingitems, this) recipelist.setadapter(adapter);
to refresh or update listview
adapter.notifydatasetchanged();
public void notifydatasetchanged ()
added in api level 1
notifies attached observers underlying data has been changed , view reflecting data set should refresh itself.
Comments
Post a Comment