java - How to set Adapter for listview with image in Android -
i have click button show listview image. unfortunately, application stops when click on button. have set custom adapter still same. here code:
public class r1 extends activity { string[] signtitle; string[] signdescription; int images[]={r.drawable.a,r.drawable.b,r.drawable.c,r.drawable.d,r.drawable.e,r.drawable. f,r.drawable.g,r.drawable.h}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.r1); resources res = getresources(); signtitle = res.getstringarray(r.array.titile); signdescription = res.getstringarray(r.array.description); listview lv = (listview) findviewbyid(r.id.listview); customlistadapter adapter = new customlistadapter(this,signtitle,images,signdescription); lv.setadapter(adapter);}
customadpater.java
public class customlistadapter extends arrayadapter<string> { context context; int[] images; string[] titlearray; string[] descriptionarray; public customlistadapter(context c, string[] title, int imgs[],string[] desc) { super(c, r.layout.activity_show,r.id.textview1,title); this.context = c; this.images = imgs; this.titlearray = title; this.descriptionarray = desc; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); view row = inflater.inflate(r.layout.activity_show, parent,false); imageview myimage = (imageview) row.findviewbyid(r.id.image); textview mytitle = (textview) row.findviewbyid(r.id.textview1); textview mydescription = (textview) row.findviewbyid(r.id.textview2); myimage.setimageresource(images[position]); mytitle.settext(titlearray[position]); mydescription.settext(descriptionarray[position]); return row; } }
if post logcat or @ least point out line produces error, better. initializing listview highly inefficient.
implementing viewholder patter helps.
optimized customlistadapter
public class customlistadapter extends arrayadapter<string> { context context; int[] images; string[] titlearray; string[] descriptionarray; public customlistadapter(context c, string[] title, int imgs[],string[] desc) { super(c, r.layout.activity_show,r.id.textview1,title); this.context = c; this.images = imgs; this.titlearray = title; this.descriptionarray = desc; // todo auto-generated constructor stub } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub viewholder viewholder; if(convertview == null) { viewholder = new viewholder(); layoutinflater inflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.activity_show, parent,false); viewholder.title = (textview)convertview.findviewbyid(r.id.textview1); viewholder.description = (textview)convertview.findviewbyid(r.id.textview2); viewholder.image = (imageview) convertview.findviewbyid(r.id.image); convertview.settag(viewholder); } else { viewholder = (viewholder) convertview.gettag(); if(titlearray[position] != null) { viewholder.image = (imageview) convertview.findviewbyid(r.id.image); viewholder.title= (textview) convertview.findviewbyid(r.id.textview1); viewholder.description = (textview) convertview.findviewbyid(r.id.textview2); viewholder.image.setimageresource(images[position]); viewholder.title.settext(titlearray[position]); viewholder.description.settext(descriptionarray[position]); } } return convertview; } private class viewholder { textview title,description; imageview image; }
}
Comments
Post a Comment