java - onClickListener in RecyclerView.ViewHolder -
i've got recyclerview.viewholder , recyclerview.adapter, need after click on item , send information item activity
.
placesadapter.java
public class placesadapter extends recyclerview.adapter<placesviewholder> { private placesactivity placesactivity; context context; private int position; list<places> places; public placesadapter(list<places> places) { this.places = places;} @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } @override public placesviewholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.places_view, viewgroup, false); placesviewholder pvh = new placesviewholder(v); return pvh; } @override public void onbindviewholder(placesviewholder personviewholder, int i) { personviewholder.name.settext(places.get(i).name); personviewholder.address.settext(places.get(i).address); picasso.with(personviewholder.itemview.getcontext()) .load(places.get(i).photo) .into(personviewholder.getphoto()); } @override public int getitemcount() { return places.size(); } }
placesviewholder.java
in line "intent.putextra(placesdetail.places_name,);"
how can send name?
public class placesviewholder extends recyclerview.viewholder { cardview cv; public textview name; public textview address; public imageview photo; public placesviewholder(final view itemview) { super(itemview); cv = (cardview)itemview.findviewbyid(r.id.cv); name = (textview)itemview.findviewbyid(r.id.person_name); address = (textview)itemview.findviewbyid(r.id.person_age); photo = (imageview)itemview.findviewbyid(r.id.person_photo); itemview.setonclicklistener(new view.onclicklistener(){ @override public void onclick(view v) { context context = v.getcontext(); intent intent = new intent(context, placesdetail.class); intent.putextra(placesdetail.places_name,); context.startactivity(intent); } }); } public textview getaddress() { return address; } public textview getname() { return name; } public imageview getphoto() { return photo; } }
this complete example of custom adapter i'm able details of particular items. in mainactivity need set adapter :
adapter = new myadapter(getapplicationcontext(), account_no, title, afn1, aln1,afn2, aln2,afn3, aln3,isavilable,waitlist,flag); adapter.notifydatasetchanged(); mrecyclerview.setadapter(adapter);
now see code custom adapter :
public class myadapter extends recyclerview.adapter<myadapter.viewholder> { string tag = "myadapter"; context context; private string[] accountno; private string[] title; private string[] afn1; private string[] aln1; private string[] afn2; private string[] aln2; private string[] afn3; private string[] aln3; private string[] isavailable; private string[] waitlist; private int flag; public static class viewholder extends recyclerview.viewholder { public textview account_no,title,afn1,afn2,aln1,aln2,color,is_available; private imageview img_android; cardview cardview; public viewholder(cardview v) { super(v); account_no = (textview)v.findviewbyid(r.id.acctno); title = (textview)v.findviewbyid(r.id.title); afn1 = (textview) v.findviewbyid(r.id.afn1); cardview = v; } } // provide suitable constructor (depends on kind of dataset) public myadapter(context context, string[] accountno,string[] title,string[] afn1,string[] aln1,string[] afn2,string[] aln2,string[] afn3,string[] aln3,string[] isavailable, string[] waitlist,int flag) { this.context = context; this.accountno = accountno; this.title = title; this.afn1 = afn1; this.aln1 = aln1; this.afn2 = afn2; this.aln2 = aln2; this.afn3 = afn3; this.aln3 = aln3; this.isavailable = isavailable; this.waitlist = waitlist; this.flag = flag; log.d(tag,afn1.tostring() +aln1+afn2+aln2+afn3+aln3.tostring()+waitlist); } @override public viewholder oncreateviewholder(viewgroup parent,int viewtype) { cardview cv = (cardview) layoutinflater.from(parent.getcontext()).inflate(r.layout.list_item, parent, false); return new viewholder(cv); } @override public void onbindviewholder(final viewholder holder, final int position) { final cardview cardview = holder.cardview; final textview accountno = (textview)cardview.findviewbyid(r.id.acctno); accountno.settext(accountno[position]); final textview titletxt = (textview)cardview.findviewbyid(r.id.title); titletxt.settext(title[position]); final textview afn1txt = (textview) cardview.findviewbyid(r.id.afn1); afn1txt.settext(afn1[position]+" "+aln1[position]); log.d(tag,waitlist[position]); cardview.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { int id = getitemviewtype(position); sharedpreferences sharedpreferences = context.getsharedpreferences(constant.mypreference,context.mode_private); sharedpreferences.editor edit = sharedpreferences.edit(); edit.putstring(constant.account,accountno[position]); edit.putstring(constant.title,title[position]); edit.putstring(constant.afn1,afn1[position]); edit.putstring(constant.aln1,aln1[position]); edit.putstring(constant.afn2,afn2[position]); edit.putstring(constant.aln2,aln2[position]); edit.putstring(constant.afn3,afn3[position]); edit.putstring(constant.aln3,aln3[position]); edit.putboolean(constant.is_available, boolean.parseboolean(isavailable[position].touppercase())); edit.putstring(constant.waitlist,waitlist[position]); log.d("waitingno2 :",""+string.valueof(waitlist[position])); edit.commit(); intent intent = new intent(v.getcontext(), detailsactivity.class); v.getcontext().startactivity(intent); } }); } // return size of dataset (invoked layout manager) @override public int getitemcount() { return accountno.length; } }
after value sharedpreferences in activity called on card click:
preferences = getsharedpreferences("myshared", context.mode_private); accountno = sharedpreferences.getstring(constant.account,null).touppercase(); title = sharedpreferences.getstring(constant.title,null).touppercase(); afn1 = sharedpreferences.getstring(constant.afn1,null).touppercase(); aln1 = sharedpreferences.getstring(constant.aln1,null).touppercase(); afn2 = sharedpreferences.getstring(constant.afn2,null).touppercase(); aln2 = sharedpreferences.getstring(constant.aln2,null).touppercase(); afn3 = sharedpreferences.getstring(constant.afn3,null).touppercase(); aln3 = sharedpreferences.getstring(constant.aln3,null).touppercase(); isavailable = sharedpreferences.getboolean(constant.is_available,isavailable); waitlist = sharedpreferences.getstring(constant.waitlist,waitlist);
now whatever want , enjoy code.
Comments
Post a Comment