android - How to set onclicklistener on cardview? -


i inflating card view through custom recyclerview adapter.now,i want know how can set click listener adpater class on su,mo,tu,we,th,fr,sa position wise.

i want array if user clicked su,mo,tu on 0 position of cardview array should follows [1,2,3]

and on 1 st position user clicked we,th,fr should array follows [4,5,6]

currently adapter have taken references of these textview , added click listener getting same array both postion if selections different.

my adapter class follows.

public class adapter1 extends recyclerview.adapter {   private final int view_item = 1; private final int view_prog = 0;   private list<string> mydataset; private static string key1; // minimum amount of items have below current scroll position // before loading more. private int visiblethreshold = 5; private int lastvisibleitem, totalitemcount; private boolean loading; private onloadmorelistener onloadmorelistener; private static string tag = "data_adapter"; private static arraylist<string> msavedtime = new arraylist<string>(); private static arraylist<string> mtimeunit = new arraylist<>(); private static string mcurrenthour; private static string mcurrentminute;  private static context context;     public alarmadapter(context context, recyclerview recyclerview, arraylist<string> mtime, arraylist<string> mtimeunit, string mcurrenthour, string mcurrentminute) {       this.msavedtime = mtime;     this.mtimeunit = mtimeunit;     this.mcurrenthour = mcurrenthour;     this.mcurrentminute = mcurrentminute;      this.context = context;     if (recyclerview.getlayoutmanager() instanceof linearlayoutmanager) {          final linearlayoutmanager linearlayoutmanager = (linearlayoutmanager) recyclerview                 .getlayoutmanager();           recyclerview                 .addonscrolllistener(new recyclerview.onscrolllistener() {                     @override                     public void onscrolled(recyclerview recyclerview,                                            int dx, int dy) {                         super.onscrolled(recyclerview, dx, dy);                          totalitemcount = linearlayoutmanager.getitemcount();                         lastvisibleitem = linearlayoutmanager                                 .findlastvisibleitemposition();                         if (!loading                                 && totalitemcount <= (lastvisibleitem + visiblethreshold)) {                             // end has been reached                             //                             if (onloadmorelistener != null) {                                 onloadmorelistener.onloadmore();                             }                             loading = true;                         }                     }                 });     } }  @override public int getitemviewtype(int position) {     return msavedtime.get(position) != null ? view_item : view_prog; }  @override public recyclerview.viewholder oncreateviewholder(viewgroup parent,                                                   int viewtype) {     recyclerview.viewholder vh;       if (viewtype == view_item) {         view v = layoutinflater.from(parent.getcontext()).inflate(                 r.layout.saved_alarm_card, parent, false);          vh = new studentviewholder(v);      } else {         view v = layoutinflater.from(parent.getcontext()).inflate(                 r.layout.more_progress_item, parent, false);          vh = new progressviewholder(v);     }      return vh; }  @override public void onbindviewholder(recyclerview.viewholder holder, int position) {     if (holder instanceof studentviewholder) {           ((studentviewholder) holder).mtimedigittext.settext(msavedtime.get(position));         ((studentviewholder) holder).mtimeunittext.settext(mtimeunit.get(position));         //  ((studentviewholder) holder).datefield.settext(mfiledate.get(position));       } else {         ((progressviewholder) holder).progressbar.setindeterminate(true);     } }  public void setloaded() {     loading = false; }  @override public int getitemcount() {     return msavedtime.size(); }  public void setonloadmorelistener(onloadmorelistener onloadmorelistener) {     this.onloadmorelistener = onloadmorelistener; }   // public class studentviewholder extends recyclerview.viewholder implements view.onclicklistener {     public textview mtimedigittext;     public textview mtimeunittext;     private switchcompat malarmbuttonpower;     private imageview malarmbellimage;      private textview msundaybutton;     private textview mmondaybutton;     private textview mtuesdaybutton;     private textview mwednesdaybutton;     private textview mthursdaybutton;     private textview mfridaybutton;     private textview msaturdaybutton;      public cardview cardview1;      public studentviewholder(view v) {         super(v);         mtimedigittext = (textview) v.findviewbyid(r.id.timedigit);         mtimeunittext = (textview) v.findviewbyid(r.id.timeunit);         malarmbellimage = (imageview) v.findviewbyid(r.id.alarmbell);         malarmbuttonpower = (switchcompat) v.findviewbyid(r.id.alarmpowerswitch);          msundaybutton = (textview) v.findviewbyid(r.id.sunday);         mmondaybutton = (textview) v.findviewbyid(r.id.monday);         mtuesdaybutton = (textview) v.findviewbyid(r.id.tuesday);         mwednesdaybutton = (textview) v.findviewbyid(r.id.wednesday);         mthursdaybutton = (textview) v.findviewbyid(r.id.thursday);         mfridaybutton = (textview) v.findviewbyid(r.id.friday);         msaturdaybutton = (textview) v.findviewbyid(r.id.saturday);           v.findviewbyid(r.id.alarmbell).setonclicklistener(this);         v.findviewbyid(r.id.sunday).setonclicklistener(this);         v.findviewbyid(r.id.monday).setonclicklistener(this);         v.findviewbyid(r.id.tuesday).setonclicklistener(this);         v.findviewbyid(r.id.wednesday).setonclicklistener(this);         v.findviewbyid(r.id.thursday).setonclicklistener(this);         v.findviewbyid(r.id.friday).setonclicklistener(this);         v.findviewbyid(r.id.saturday).setonclicklistener(this);              malarmbuttonpower.setchecked(true);         if (malarmbuttonpower.ischecked()) {             mbellcount = 2;             malarmbellimage.setimagedrawable(contextcompat.getdrawable(context, r.drawable.bell_touch));             toast.maketext(context, "alarm activated!!", toast.length_short).show();         } else {             malarmbellimage.setimagedrawable(contextcompat.getdrawable(context, r.drawable.bell));             toast.maketext(context, "first activate alarm", toast.length_short).show();         }         malarmbuttonpower.setoncheckedchangelistener(new compoundbutton.oncheckedchangelistener() {             @override             public void oncheckedchanged(compoundbutton buttonview, boolean ischecked) {                 switch (buttonview.getid()) {                      case r.id.alarmpowerswitch:                          if (!ischecked) {                             malarmbellimage.setimagedrawable(contextcompat.getdrawable(context, r.drawable.bell));                             toast.maketext(context, "alarm off!!", toast.length_short).show();                          } else {                             //  alarmreceiver.stopringtone();                             // alarmmanager.cancel(pendingintent);                             log.d("myactivity", "alarm off");                             malarmbellimage.setimagedrawable(contextcompat.getdrawable(context, r.drawable.bell_touch));                             toast.maketext(context, "alarm on!!", toast.length_short).show();                         }                         break;                      default:                         break;                 }             }         });      }       @override     public void onclick(view v) {          switch (v.getid()) {              case r.id.alarmbell:                  mbellcount = mbellcount + 1;                  if (mbellcount % 2 == 0) {                     malarmbellimage.setimagedrawable(contextcompat.getdrawable(context, r.drawable.bell_touch));                 } else {                     malarmbellimage.setimagedrawable(contextcompat.getdrawable(context, r.drawable.bell));                 }                  break;              case r.id.sunday:                     msuncount = msuncount + 1;               //  mdaysarraylist.add(1);                 if (msuncount % 2 == 0) {                     msundaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                  } else {                     msundaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                   //  mdaysarraylist.clear();                 }                 break;              case r.id.monday:                 mmoncount = mmoncount + 1;               //  mdaysarraylist.add(2);                 if (mmoncount % 2 == 0) {                     mmondaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                 } else {                     mmondaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                     //mdaysarraylist.remove(1);                    // mdaysarraylist.clear();                 }                 break;             case r.id.tuesday:                  mtuecount = mtuecount + 1;               //  mdaysarraylist.add(3);                 if (mtuecount % 2 == 0) {                     mtuesdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                 } else {                     mtuesdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                     // mdaysarraylist.remove(2);                     //mdaysarraylist.clear();                 }                 break;              case r.id.wednesday:                  mwedcount = mwedcount + 1;              //   mdaysarraylist.add(4);                 if (mwedcount % 2 == 0) {                     mwednesdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                 } else {                     mwednesdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                     // mdaysarraylist.remove(3);                     //mdaysarraylist.clear();                 }                 break;              case r.id.thursday:                  mthucount = mthucount + 1;             //    mdaysarraylist.add(5);                 if (mthucount % 2 == 0) {                     mthursdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                 } else {                      mthursdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                     // mdaysarraylist.remove(4);                     //mdaysarraylist.clear();                 }                 break;              case r.id.friday:                  mfricount = mfricount + 1;              //   mdaysarraylist.add(6);                 if (mfricount % 2 == 0) {                     mfridaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                 } else {                     mfridaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                     //mdaysarraylist.remove(5);                    // mdaysarraylist.clear();                 }                 break;              case r.id.saturday:                  msatcount = msatcount + 1;               //  mdaysarraylist.add(7);                 if (msatcount % 2 == 0) {                     msaturdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorprofileprimary));                 } else {                     msaturdaybutton.settextcolor(contextcompat.getcolor(context, r.color.colorsecondary));                     //mdaysarraylist.remove(6);                  //   mdaysarraylist.clear();                 }                 break;           }       } }  public static class progressviewholder extends recyclerview.viewholder {     public progressbar progressbar;      public progressviewholder(view v) {         super(v);         progressbar = (progressbar) v.findviewbyid(r.id.progressbar1);     } } } 

any welcome.thanks

you can position this:

@override public void onclick(view view) {     int position = getadapterposition();     //do depending on position     //... } 

Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -