Android - Can change button color in listview -
can change button color in listview simplecursoradapter or use object can use color stripe show type , how can use problem.
public class coloradapter extends simplecursoradapter { private context context; public coloradapter(context context, int layout, cursor c, string[] from, int[] to) { super(context, layout, c, from, to); this.context = context; } public int getcount() { // todo auto-generated method stub return myarrlist.size(); } public object getitem(int position) { // todo auto-generated method stub return position; } public long getitemid(int position) { // todo auto-generated method stub return position; } public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); convertview = inflater.inflate(r.layout.event_list, null); } button btype = (button) findviewbyid(r.id.ctype); if (myarrlist.get(position).get("eventtype") .equalsignorecase("red")) { btype.setbackgroundcolor(color.red); } else if (myarrlist.get(position).get("eventtype") .equalsignorecase("black")) { btype.setbackgroundcolor(color.black); } else { btype.setbackgroundcolor(color.blue); } textview tname = (textview) convertview.findviewbyid(r.id.ename); tname.settext(myarrlist.get(position).get("eventname")); textview tdetail = (textview) convertview .findviewbyid(r.id.edetail);// ãÊè¢éÍÁÙÅ·ÕÅÐÊèǹ tdetail.settext(myarrlist.get(position).get("eventdetail")); return convertview; } }
my code not work can me...
you suppose access button
in getview()
using convertview
instance. after can access , change button
color.
change below line
button btype = (button) findviewbyid(r.id.ctype);
to below:
button btype = (button) convertview.findviewbyid(r.id.ctype);
also return item id arraylist in below method:
public object getitem(int position) { return myarrlist.get(position); }
Comments
Post a Comment