Android custom adapter for listview -
i have custom listview i'm displaying images json input. http://i.imgur.com/kile5.png i'm kind of confused when pass in value number of stars in yellow colour items. value passed in basketball 3rd item in list 1,2,3 yellow stars meaning item 1 should displaying 1 star, item 2, 2 stars , item 3, 3 stars.
it seems item 1 getting value of item 3. reading left of item, star1, star2 , on until star5.
refer following code custom adapter:
public class myadapter extends arrayadapter<applicationitem> { int resource; string response; context context; public imageloader imageloader; private activity activity; private static layoutinflater inflater=null; //initialize adapter public myadapter(context context, int resource, activity activity, list<applicationitem> items) { super(context, resource, items); this.resource=resource; imageloader=new imageloader(activity.getapplicationcontext()); inflater = (layoutinflater)activity.getsystemservice(context.layout_inflater_service); } public static class viewholder{ public textview publisher; public textview application; public textview price; public imageview image; public imageview star1, star2, star3, star4, star5; } @override public view getview(int position, view convertview, viewgroup parent) { applicationitem app = getitem(position); view vi=convertview; viewholder holder; if(convertview==null) { vi = inflater.inflate(r.layout.item, null); holder=new viewholder(); holder.publisher=(textview)vi.findviewbyid(r.id.publisher); holder.application=(textview)vi.findviewbyid(r.id.application); holder.price=(textview)vi.findviewbyid(r.id.price); holder.image=(imageview)vi.findviewbyid(r.id.icon); holder.star1 = (imageview)vi.findviewbyid(r.id.itemstar1); holder.star2 = (imageview)vi.findviewbyid(r.id.itemstar2); holder.star3 = (imageview)vi.findviewbyid(r.id.itemstar3); holder.star4 = (imageview)vi.findviewbyid(r.id.itemstar4); holder.star5 = (imageview)vi.findviewbyid(r.id.itemstar5); vi.settag(holder); } else { holder=(viewholder)vi.gettag(); } holder.publisher.settext(app.publisher); holder.application.settext(app.application); holder.price.settext(app.price); holder.image.settag(app.imageurl); imageloader.displayimage(app.imageurl, activity, holder.image); switch (integer.valueof(app.rating)) { case 5: holder.star1.setimageresource(r.drawable.star_full); holder.star2.setimageresource(r.drawable.star_full); holder.star3.setimageresource(r.drawable.star_full); holder.star4.setimageresource(r.drawable.star_full); holder.star5.setimageresource(r.drawable.star_full); break; case 4: holder.star1.setimageresource(r.drawable.star_full); holder.star2.setimageresource(r.drawable.star_full); holder.star3.setimageresource(r.drawable.star_full); holder.star4.setimageresource(r.drawable.star_full); break; case 3: holder.star1.setimageresource(r.drawable.star_full); holder.star2.setimageresource(r.drawable.star_full); holder.star3.setimageresource(r.drawable.star_full); break; case 2: holder.star1.setimageresource(r.drawable.star_full); holder.star2.setimageresource(r.drawable.star_full); break; case 1: holder.star1.setimageresource(r.drawable.star_full); break; } return vi; }
}
and have following xml describe layout of star1 star5
<linearlayout android:id="@+id/itemrating" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@id/application" android:layout_alignright="@id/publisher" android:layout_below="@id/price"> <imageview android:id="@+id/itemstar1" android:src="@drawable/star_empty" android:layout_width="18dip" android:scaletype="fitxy" android:layout_height="20dip" /> <imageview android:id="@+id/itemstar2" android:src="@drawable/star_empty" android:layout_width="18dip" android:scaletype="fitxy" android:layout_height="20dip" /> <imageview android:id="@+id/itemstar3" android:src="@drawable/star_empty" android:layout_width="18dip" android:scaletype="fitxy" android:layout_height="20dip" /> <imageview android:id="@+id/itemstar4" android:src="@drawable/star_empty" android:layout_width="18dip" android:scaletype="fitxy" android:layout_height="20dip" /> <imageview android:id="@+id/itemstar5" android:src="@drawable/star_empty" android:layout_width="18dip" android:scaletype="fitxy" android:layout_height="20dip" /> </linearlayout>
edit: part put in number of stars here json block not repeating i've put log check:
(int = 0; < ja.length(); i++) { jsonobject jo = (jsonobject) ja.get(i); applicationitem ai = new applicationitem(); ai.rating = jo.getstring("star"); m_apps.add(ai); m_adapter.notifydatasetchanged(); }
Comments
Post a Comment