java - Android - RecyclerView onClick not work -
i try change background of imageview
clicking them, isn't work. i've in viewholder
i
attempt invoke virtual method ..streamviewholder.getadapterposition() on null object erference
import java.util.list; import de.caglar.aqua.r; import de.caglar.aqua.objects.streamobject; import de.caglar.aqua.views.circularimageview; import android.support.v7.widget.cardview; import android.support.v7.widget.recyclerview; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.imageview; import android.widget.textview; import android.widget.toast; public class streamadapter extends recyclerview.adapter<streamadapter.streamviewholder> implements view.onclicklistener { public static list<streamobject> streams; public streamadapter(list<streamobject> s){ streams = s; } public static class streamviewholder extends recyclerview.viewholder { cardview cardview; public textview streamtitle; public textview streamtext; public textview streamdescription; public circularimageview streamphoto; public imageview streamlike; public streamviewholder(view itemview) { super(itemview); cardview = (cardview) itemview.findviewbyid(r.id.stream_cardview); streamtitle = (textview) itemview.findviewbyid(r.id.stream_title); streamtext = (textview) itemview.findviewbyid(r.id.stream_text); streamdescription = (textview) itemview.findviewbyid(r.id.stream_description); streamphoto = (circularimageview) itemview.findviewbyid(r.id.stream_photo); streamlike = (imageview) itemview.findviewbyid(r.id.stream_like); } } @override public int getitemcount() { return streams.size(); } @override public void onbindviewholder(streamviewholder streamviewholder, int i) { streamviewholder.streamtitle.settext(streams.get(i).title); streamviewholder.streamtext.settext(streams.get(i).text); streamviewholder.streamdescription.settext(streams.get(i).description); streamviewholder.streamphoto.setbackground(streams.get(i).photo); if(streams.get(i).isliked) { streamviewholder.streamlike.setimageresource(r.drawable.ic_action_like_active); } else { streamviewholder.streamlike.setimageresource(r.drawable.ic_action_like); } streamviewholder.streamlike.setonclicklistener(this); } @override public void onclick(view v) { try { if (v instanceof imageview) { streamviewholder holder = (streamviewholder) v.gettag(); int position = holder.getadapterposition(); if(streams.get(position).isliked) { streams.get(position).isliked = false; holder.streamlike.setimageresource(r.drawable.ic_action_like); } else { streams.get(position).isliked = true; holder.streamlike.setimageresource(r.drawable.ic_action_like_active); } } } catch (exception e) { toast.maketext(v.getcontext(), e.getmessage().tostring(), toast.length_long).show(); } } @override public streamviewholder oncreateviewholder(viewgroup viewgroup, int i) { view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.cardview_stream, viewgroup, false); streamviewholder pvh = new streamviewholder(v); return pvh; } @override public void onattachedtorecyclerview(recyclerview recyclerview) { super.onattachedtorecyclerview(recyclerview); } }
put in onbindviewholder
method
streamviewholder.streamlike.settag(streamviewholder)
Comments
Post a Comment