android - recyclerview, on click Highlight and make others not highlighted -


i have recyclerview of credit cards, if click on 1 want change background resource of 1 , make other ones default, reason have been fighting 5 hours , cant right.

here failing class

    public class checkoutcardrecycler extends recyclerview.adapter<checkoutcardrecycler.cardviewholder>{      private list<card> checkoutcard;     private int screenwidth;     private int selected;           public checkoutcardrecycler(activity activity, list<card> checkoutcard){           display display = activity.getwindowmanager().getdefaultdisplay();         point size = new point();         display.getsize(size);         this.screenwidth = size.x;         this.checkoutcard = checkoutcard;         selected=-1;       }        public static class cardviewholder extends recyclerview.viewholder {          cardview card;          linearlayout row;         static textview etcardnumber;         static textview etname;         static textview etdate;         static linearlayout creditcard;            cardviewholder(view itemview) {             super(itemview);             card = (cardview)itemview.findviewbyid(r.id.card_view);             etcardnumber = (textview)itemview.findviewbyid(r.id.etcardnumber);             etname = (textview)itemview.findviewbyid(r.id.etname);             etdate = (textview)itemview.findviewbyid(r.id.etdate);             creditcard = (linearlayout) itemview.findviewbyid(r.id.creditcard);          }     }      @override     public int getitemcount() {         return checkoutcard.size();     }      public int getselected() {         return selected;     }      public int getpaymentmethod(int i) {         return integer.parseint(checkoutcard.get(i).getid());     }       @override     public cardviewholder oncreateviewholder(viewgroup viewgroup, int i) {         view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.row_checkout_cards, viewgroup, false);         cardviewholder pvh = new cardviewholder(v);          return pvh;     }      @override     public void onbindviewholder(final cardviewholder cardviewholder, final int i) {         cardviewholder.etcardnumber.settext("**** **** **** " + checkoutcard.get(i).getlast_four_digits());         cardviewholder.etname.settext(checkoutcard.get(i).getname());         cardviewholder.etdate.settext(checkoutcard.get(i).getexpiry_month() +"/"+checkoutcard.get(i).getexpiry_year().substring(2,4));         if (selected<0)             if (checkoutcard.get(i).getdefaultcard()==1){                 selected=i;             }          cardviewholder.creditcard.setonclicklistener(new view.onclicklistener()         {             @override             public void onclick(view v)             {                  if (i!=selected) {                      checkoutcard.get(selected).setdefaultcard(0);                     checkoutcard.get(i).setdefaultcard(1);                     selected=-1;                      notifydatasetchanged();                     }             }         });           if (selected==i){             if (checkoutcard.get(i).getbrand().tolowercase().equals("visa"))                 cardviewholder.creditcard.setbackgroundresource(r.drawable.paymentscreenvisaselected);             else if (checkoutcard.get(i).getbrand().tolowercase().equals("mastercard"))                 cardviewholder.creditcard.setbackgroundresource(r.drawable.paymentscreenmastercardselected);             else                 cardviewholder.creditcard.setbackgroundresource(r.drawable.paymentscreenselected);         }         else {              if (checkoutcard.get(i).getbrand().tolowercase().equals("visa"))                 cardviewholder.creditcard.setbackgroundresource(r.drawable.paymentscreenvisa);             else if (checkoutcard.get(i).getbrand().tolowercase().equals("mastercard"))                 cardviewholder.creditcard.setbackgroundresource(r.drawable.paymentscreenmastercard);             else                 cardviewholder.creditcard.setbackgroundresource(r.drawable.paymentscreen);          }       }      @override     public void onattachedtorecyclerview(recyclerview recyclerview) {         super.onattachedtorecyclerview(recyclerview);      }     } 

ruffly adapter should this:

public class cardadapter extends recyclerview.adapter<cardadapter.cardviewholder> {     private context context;     private list<card> checkoutcard;     private int screenwidth; // think shouldn't calculate width in adapter before create it.     private card selected;      public cardadapter(context context, list<card> checkoutcard, int screenwidth) {         this.context = context;         this.screenwidth = screenwidth;         this.checkoutcard = checkoutcard;     }      protected context getcontext() {         return context;     }      protected card getitem(int position){      }      public card getselected() {         return selected;     }      public void setselected(card selected) {         this.selected = selected;     }      @override     public cardviewholder oncreateviewholder(viewgroup parent, int viewtype) {         return new cardviewholder(layoutinflater.from(getcontext()).inflate(r.layout.row_checkout_cards, parent, false));     }      @override     public void onbindviewholder(cardviewholder holder, int position) {         card currentcard = getitem(position);         holder.bind(currentcard, currentcard == selected);      }      @override     public int getitemcount() {         return 0;     }      public static class cardviewholder extends recyclerview.viewholder {         cardview card;         linearlayout row;         static textview etcardnumber;         static textview etname;         static textview etdate;         static linearlayout creditcard;          cardviewholder(view itemview) {             super(itemview);             card = (cardview)itemview.findviewbyid(r.id.card_view);             etcardnumber = (textview)itemview.findviewbyid(r.id.etcardnumber);             etname = (textview)itemview.findviewbyid(r.id.etname);             etdate = (textview)itemview.findviewbyid(r.id.etdate);             creditcard = (linearlayout) itemview.findviewbyid(r.id.creditcard);          }          public void bind(card card, boolean isselected){             etcardnumber.settext("**** **** **** " + card.getlast_four_digits());             etname.settext(card.getname());             etdate.settext(card.getexpiry_month() +"/"+card.getexpiry_year().substring(2,4));             if (isselected)                 if (card.getdefaultcard()==1){                     selected=i;                 }              creditcard.setonclicklistener(new view.onclicklistener()             {                 @override                 public void onclick(view v)                 {                      if (i!=selected) {                          checkoutcard.get(selected).setdefaultcard(0);                         card.setdefaultcard(1);                         selected=-1;                          notifydatasetchanged();                         }                 }             });               if (selected==i){                 if (card.getbrand().tolowercase().equals("visa"))                     creditcard.setbackgroundresource(r.drawable.paymentscreenvisaselected);                 else if (card.getbrand().tolowercase().equals("mastercard"))                     creditcard.setbackgroundresource(r.drawable.paymentscreenmastercardselected);                 else                     creditcard.setbackgroundresource(r.drawable.paymentscreenselected);             }             else {                  if (card.getbrand().tolowercase().equals("visa"))                     creditcard.setbackgroundresource(r.drawable.paymentscreenvisa);                 else if (card.getbrand().tolowercase().equals("mastercard"))                     creditcard.setbackgroundresource(r.drawable.paymentscreenmastercard);                 else                     creditcard.setbackgroundresource(r.drawable.paymentscreen);              }         }     } } 

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) -