android - Dynamically change custom list item background -


i creating custom list view favorite functionality, don't know how change favorite image background on click. when change background of favorite icon automatically change background of favorite image background @ time of scrolling. please check below code :

public class customarrayadapter extends baseadapter {     private activity activity;     private layoutinflater inflater = null;     arraylist<customlist> list;     decimalformat formatter = new decimalformat("#,##,###");  public customarrayadapter(activity a, arraylist<customlist> list) {     activity = a;     inflater = (layoutinflater) activity             .getsystemservice(context.layout_inflater_service);     this.list = list;  }  public int getcount() {     return list.size(); }  public object getitem(int position) {     return position; }  public long getitemid(int position) {     return position; }  @override public view getview(final int position, view convertview,         final viewgroup parent) {      textview txt_unit, txt_state, txt_price, term_left, customr;     textview install_date;     final imageview fav;     view view = convertview;      if (convertview == null)         view = inflater.inflate(r.layout.list_item, null);      customr = (textview) view.findviewbyid(r.id.customr);     txt_state = (textview) view.findviewbyid(r.id.txt_state);     install_date = (textview) view.findviewbyid(r.id.install_date);     term_left = (textview) view.findviewbyid(r.id.term_left);     txt_price = (textview) view.findviewbyid(r.id.txt_price);     fav = (imageview) view.findviewbyid(r.id.fav);     txt_unit = (textview) view.findviewbyid(r.id.txt_unit);      fav.setonclicklistener(new onclicklistener() {          @override         public void onclick(view v) {             // todo auto-generated method stub              // fav = (imageview)v.findviewbyid(r)              fav.setbackgroundresource(r.drawable.favourite_select);              toast.maketext(activity, "click", 1).show();          }     });      // set values     customr.settext(list.get(position).getcustomer());     txt_state.settext(list.get(position).getstate_name());     install_date.settext(list.get(position).getinstall_date());     term_left.settext(list.get(position).gettrem_left());     string price = formatter.format(integer.parseint(list.get(position)             .getrupees()));        return view; } } 

first, need implement adapter on viewholder pattern:

@override public view getview(int position, view convertview, viewgroup parent) {     // todo auto-generated method stub     viewholdler holder = null;     if (convertview == null) {         convertview = layoutinflater.from(ctx).inflate(                 r.layout.frag_home_gridview_item, null, false);         holder = new viewholdler();         holder.iv = (imageview) convertview                 .findviewbyid(r.id.gridview_item_label);         holder.tv = (textview) convertview                 .findviewbyid(r.id.gridview_item_name);         convertview.settag(holder);     } else {         holder = (viewholdler) convertview.gettag();     }     holder.tv.settext(getitem(position));     holder.iv.setimageresource(this.ids[position]);     return convertview; }  private class viewholdler {     imageview iv;     textview tv; } 

second, use partial refreshment mechanism change target view's background:

private void refreshpartially(int position){     int firstvisibleposition = listview.getfirstvisibleposition();     int lastvisibleposition = listview.getlastvisibleposition();     if(position>=firstvisibleposition && position<=lastvisibleposition){         view view = listview.getchildat(position - firstvisibleposition);         if(view.gettag() instanceof viewholder){             viewholder vh = (viewholder)view.gettag();             //holder.play.setbackgroundresource(resid);//do here.             ...         }     } } 

third, add adapterview.onitemclicklistener listview:

mlistview.setonitemclicklistener(new adapterview.onitemclicklistener() {         @override         public void onitemclick(adapterview<?> parent, view view, int position, long id) {              refreshpartially(position);         } }); 

Comments