android - how to use arrayadapter.getview method -
here defined siteadapter , extends arrayadapter:
public class siteadapter extends arrayadapter<site> { private int resourceid; private list<site> sites = null; private context context; public siteadapter(context context, int resource, list<site> objects) { super(context, resource, objects); this.context = context; this.resourceid = resource; this.sites = objects; } @override public site getitem(int position) { return sites.get(position); } @override public int getcount() { return sites.size(); } //get viewpage inflate site_layout.xml file @override public view getview(int position, view convertview, viewgroup parent) { site site = getitem(position); view view = convertview; if (view == null) { layoutinflater vi = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view = vi.inflate(resourceid, null); } //this place need whole widget in site_layout.xml file imageview i1 = (imageview) view.findviewbyid(r.id.thumbnail); textview address = (textview) view.findviewbyid(r.id.address); textview t1 = (textview) view.findviewbyid(r.id.name); textview t3 = (textview) view.findviewbyid(r.id.distance); i1.setimageresource(site.getimageid()); address.settext(site.getaddress()); string name = site.getname(); string result = parse(name); //set name of view t1.settext(name); t3.settext("<" + site.getdistance() + "m"); return view; }
and in mainactivity, set listview using site adapter, , allocated r.drawable.blank value imageview , need change imageview, each row view through siteadapter getview method, view returned question why imageview inside such view can't replace new value(image), result turn out previous r.drawable.blank imageview:
here mainactivity snippet:
view view = siteadapter.getview(2, null, listview); imageview image1 = ((imageview)view.findviewbyid(r.id.thumbnail)); image1.invalidate(); image1.setimageresource(r.drawable.amap_end); siteadapter.notifydatasetchanged();
the point not have call getview()
activity . called adapter .
now if want change imageview
, should change sites list links adapter activity , call notifydatasetchanged()
adapter .
activity :
@override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); // setupviews siteadapter adapter = new siteadapter(context,list<site> sites); listview.setadapter(adapter); // changing imageview site site = sites.get(position); site.setimage(r.drawable.*); // updating site list new image sites.set(position,site); // refresh listview adapter.notifydatasetchanged();
however , consider use recycleview
instead of listview
.
Comments
Post a Comment