java - How do I create array adapter with Image array -
i have 2 arrays like:
array 1 :
string[] web = {"google plus","twitter","windows","bing","itunes","wordpress","drupal"} ;
array 2 :
string[] webimage = {"@drawable/img1","@drawable/img2","@drawable/img3","@drawable/img4","@drawable/img5","@drawable/img6","@drawable/img7"} ;
and want create arrayadapter
uses array1 textview
, uses array2 icon of row
arrayadapter<string> adapter = new arrayadapter<string>(this,r.layout.single_row,r.id.textview,array);
you create class hold string , drawable resource
public class item{ private final string text; private final int icon; public item(final string text, final int icon){ this.text = text; this.icon = icon; } public string gettext(){ return text; } public drawable geticon(final context context){ return context.getresources().getdrawable(this.icon) } }
and create array of items
item[] items = new item[1]; item[0] = new item("google plus",r.drawable.img1); //...etc
create custom arrayadapter item
public class itemadapter extends arrayadapter<item> { private context context; public itemadapter(context context, item[] items) { super(context, 0, items); this.context = context; } @override public view getview(int position, view convertview, viewgroup parent) { // data item position item item = getitem(position); // check if existing view being reused, otherwise inflate view if (convertview == null) { convertview = layoutinflater.from(getcontext()).inflate(r.layout.item_row, parent, false); } // lookup view data population textview tvtext = (textview) convertview.findviewbyid(r.id.tvtext); imageview ivicon = (imageview) convertview.findviewbyid(r.id.ivicon); // populate data template view using data object tvtext.settext(item.gettext()); ivicon.setimagedrawable(item.getdrawable(this.context)); // return completed view render on screen return convertview; } }
in example above r.layout.item_row
layout have create containing textview
id tvtext
, imageview id ivicon
.
Comments
Post a Comment