android - Set onClickListener on the RelativeLayout contained by CardView -


i trying set onclicklistener on each of cardview elements. since not able set onclicklistener on cards (see here), thought setting relativelayout contained cards. right using native cardview library , applied this tutorial.

here's mainactivity's oncreate:

@override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main);      recyclecardview = (recyclerview) findviewbyid(r.id.cardlist);     recyclecardview.sethasfixedsize(true);      linearlayoutmanager llmanager = new linearlayoutmanager(this);     llmanager.setorientation(linearlayoutmanager.vertical);     recyclecardview.setlayoutmanager(llmanager);      data = createlist(9);      contactadapter ctadapter = new contactadapter(data);     //...     recyclecardview.setadapter(ctadapter); 

this item_card_view xml:

 <?xml version="1.0" encoding="utf-8"?>   <android.support.v7.widget.cardview        xmlns:card_view="http://schemas.android.com/apk/res-auto"        xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/card_view"        android:layout_width="match_parent" android:layout_height="match_parent"        card_view:cardcornerradius="4dp"        android:layout_margin="5dp">      <relativelayout android:id="@+id/nativecardlayout"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:clickable="true"        android:background="?android:selectableitembackground">                <!--some more layout-->      </relativelayout>  </android.support.v7.widget.cardview> 

here's contactadapter's oncreateviewholder:

@override public contactviewholder oncreateviewholder(final viewgroup viewgroup, int i) {     view itemview = layoutinflater.from(viewgroup.getcontext())                             .inflate(r.layout.item_card_view, viewgroup, false);      relativelayout cardlayout = (relativelayout)               viewgroup.findviewbyid(r.id.nativecardlayout); //nullpointerexception here     cardlayout.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {             toast.maketext(viewgroup.getcontext(), "clickable card", toast.length_long).show();         }     });     return new contactviewholder(itemview); } 

my problem relativelayout trying load, inside cardview element, null. therefore nullpointerexception.

how can set onclicklistener on each relativelayout? possible?

see below edit, logical mistake:

relativelayout cardlayout = (relativelayout)itemview.findviewbyid(r.id.nativecardlayout); 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -