xamarin.android - Xamarin replace image imageview resource in listview -


i'm beginner in xamarin. have listview, there 1 textview , 1 imageview in each row. wrote adatpter it, activity, everything. goal is, when click item in listview, image changes other one.

data class:

class otherlabellistdata {     private string otherlabel;     private int image;      public otherlabellistdata(string otherlabel, int image)     {         this.otherlabel = otherlabel;         this.image = image;     }      public string otherlabel     {         { return otherlabel; }     }      public int image     {         { return image; }     } } 

here holder class:

class otherholder {     public textview labeltxt;     public imageview iconimg;      public otherholder(view itemview)     {         labeltxt = itemview.findviewbyid<textview>(resource.id.othermessagelabel);         iconimg = itemview.findviewbyid<imageview>(resource.id.otherlabelicon);     } } 

adapter:

class otherlabellistadapter : baseadapter<otherlabellistdata> {     private javalist<otherlabellistdata> mitems;     private context mcontext;     private layoutinflater inflater;      public otherlabellistadapter(context context, javalist<otherlabellistdata> items)     {         this.mitems = items;         this.mcontext = context;     }      public override java.lang.object getitem(int position)     {         return mitems.get(position);     }       public override int count     {                 {             return mitems.size();         }     }       public override long getitemid(int position)     {         return position;     }       public override otherlabellistdata this[int position]     {                 {             return mitems[position];         }     }       public override view getview(int position, view convertview, viewgroup parent)     {          if(inflater == null)         {             inflater = (layoutinflater)mcontext.getsystemservice(context.layoutinflaterservice);         }          if(convertview == null)         {             convertview = inflater.inflate(resource.layout.otherlabelviewrow, parent, false);         }          otherholder holder = new otherholder(convertview);         holder.labeltxt.text = mitems[position].otherlabel;         holder.iconimg.setimageresource(mitems[position].image);          return convertview;     } } 

the activity:

[activity(label = "activity1", mainlauncher = false, theme = "@style/theme.mt")] public class activity_otherscreen : android.support.v4.app.fragmentactivity {     private listview mlistview;     private otherlabellistadapter adapter;     javalist<otherlabellistdata> list;     protected override void oncreate(bundle savedinstancestate)     {         base.oncreate(savedinstancestate);          setcontentview(resource.layout.other_layout);          mlistview = findviewbyid<listview>(resource.id.otherlist);          adapter = new otherlabellistadapter(this, getothers());          mlistview.adapter = adapter;          mlistview.itemclick += mlistview_itemclick;     }       private javalist<otherlabellistdata> getothers()     {         list = new javalist<otherlabellistdata>();          otherlabellistdata ot;          ot = new otherlabellistdata("label 1", resource.drawable.send2display);         list.add(ot);          ot = new otherlabellistdata("label 2", resource.drawable.send2display);         list.add(ot);          ot = new otherlabellistdata("label 3", resource.drawable.send2display);         list.add(ot);          return list;     }      private void mlistview_itemclick(object sender, adapterview.itemclickeventargs e)     {         //..         ???     } } 

i've tried more solution, images don't chgange in real when click. please, give me idea! thanks!

if want change whne click, have add click event when instanciating convertview item, :

 public override view getview (int position, view convertview, viewgroup parent) {     if(convertview == null)     {         convertview = inflater.inflate(resource.layout.otherlabelviewrow, parent, false);         convertview.click += (object sender, eventargs e) => {             int p = (int)(sender view).tag;             changeimageresource(convertview, p);         };     }     convertview.tag = position; }  void changeimageresource(view convertview, int position) {     otherholder holder = new otherholder(convertview);     holder.iconimg.setimageresource(mitems[position].image); } 

it important add click event when instanciating convertview, there 1 item in listview. then, tag thing avoid incorrect calling. if listview containing many items , have scroll, may causes error in position it's changing when scrolling. using tag method, ensure call right one.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -