java - Referencing context from a Recycle View Adapter -


i have created recycle view adapter, uses cards. in every card, want dynamically add text views linear layout inside card.

however, cannot code run

here rv adapter:

public class rvadapter extends recyclerview.adapter<rvadapter.eventviewholder>{      context context;       list<events> events;     calculations calculations = new calculations();     rvadapter(list<events> events){         this.events = events;     }      @override     public int getitemcount() {         return events.size();     }      @override     public eventviewholder oncreateviewholder(viewgroup viewgroup, int i) {         view v = layoutinflater.from(viewgroup.getcontext()).inflate(r.layout.card_view, viewgroup, false);         eventviewholder pvh = new eventviewholder(v);         return pvh;     }     @override     public void onbindviewholder(eventviewholder eventviewholder, int i) {         eventviewholder.eventname.settext(events.get(i).getname());         eventviewholder.eventlocation.settext(events.get(i).getlocationname());         eventviewholder.eventdate.settext(calculations.unixtimeconverter(events.get(i).getunixtimestamp())[0]);         eventviewholder.eventtime.settext(calculations.unixtimeconverter(events.get(i).getunixtimestamp())[1]);          //todo(2):change code dynamically add textviews proper parameters linearlayout;         //eventviewholder.eventparticipants.settext(calculations.participantconcatenation(events.get(i).getparticipants()));         (string x: events.get(i).getparticipants()) {             textview tv=new textview(context);             linearlayout.layoutparams params=new linearlayout.layoutparams                     (linearlayout.layoutparams.wrap_content, linearlayout.layoutparams.wrap_content);             params.leftmargin=50;             params.topmargin=i*50;             tv.settext(x);             tv.settextsize((float) 20);             tv.setpadding(20, 50, 20, 50);             tv.setlayoutparams(params);             eventviewholder.eventparticipants.addview(tv) ;         }     }     @override     public void onattachedtorecyclerview(recyclerview recyclerview) {         super.onattachedtorecyclerview(recyclerview);     }       public static class eventviewholder extends recyclerview.viewholder {         cardview cv;         textview eventname;         textview eventtime;         textview eventlocation;         textview eventdate;          //todo(1):change type of eventparticipants linear layout;         linearlayout eventparticipants;           eventviewholder(view itemview) {             super(itemview);             cv = itemview.findviewbyid(r.id.cardviewitem);             eventname = itemview.findviewbyid(r.id.event_name);             eventtime = itemview.findviewbyid(r.id.event_time);             eventdate = itemview.findviewbyid(r.id.event_date);             eventlocation = itemview.findviewbyid(r.id.event_location);             eventparticipants = itemview.findviewbyid(r.id.event_participants);          }      }  } 

the error seems @ place instantiate new textview , pass in context. have tried passing "this" it, causes build time error saying:

textview(android.context.context) in textview cannot applied to(...rvadapter)

i have tried passing a

mainactivity.this 

, causes build time error saying "mainactivity.this not enclosing class."

if pass in context, causes run time nullpointerexception error:

java.lang.nullpointerexception: attempt invoke virtual method 'android.content.res.resources android.content.context.getresources()' on null object reference                                                                                     @ android.view.viewconfiguration.get(viewconfiguration.java:359)                                                                                     @ android.view.view.<init>(view.java:3587)                                                                                     @ android.view.view.<init>(view.java:3682)                                                                                     @ android.widget.textview.<init>(textview.java:637)                                                                                     @ android.widget.textview.<init>(textview.java:632)                                                                                     @ android.widget.textview.<init>(textview.java:628)                                                                                     @ android.widget.textview.<init>(textview.java:624)                                                                                     @ com.example.android.gatheraround.rvadapter.onbindviewholder(rvadapter.java:56)                                                                                     @ com.example.android.gatheraround.rvadapter.onbindviewholder(rvadapter.java:24)                                                                                     @ android.support.v7.widget.recyclerview$adapter.onbindviewholder(recyclerview.java:6400)                                                                                     @ android.support.v7.widget.recyclerview$adapter.bindviewholder(recyclerview.java:6433)                                                                                     @ android.support.v7.widget.recyclerview$recycler.trybindviewholderbydeadline(recyclerview.java:5377)                                                                                     @ android.support.v7.widget.recyclerview$recycler.trygetviewholderforpositionbydeadline(recyclerview.java:5640)                                                                                     @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:5482)                                                                                     @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:5478)                                                                                     @ android.support.v7.widget.linearlayoutmanager$layoutstate.next(linearlayoutmanager.java:2215)                                                                                     @ android.support.v7.widget.linearlayoutmanager.layoutchunk(linearlayoutmanager.java:1542)                                                                                     @ android.support.v7.widget.linearlayoutmanager.fill(linearlayoutmanager.java:1502)                                                                                     @ android.support.v7.widget.linearlayoutmanager.onlayoutchildren(linearlayoutmanager.java:595)                                                                                     @ android.support.v7.widget.recyclerview.dispatchlayoutstep2(recyclerview.java:3625)                                                                                     @ android.support.v7.widget.recyclerview.onmeasure(recyclerview.java:3067)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535)                                                                                     @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436)                                                                                     @ android.widget.linearlayout.measurehorizontal(linearlayout.java:1083)                                                                                     @ android.widget.linearlayout.onmeasure(linearlayout.java:615)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.support.v4.widget.nestedscrollview.measurechildwithmargins(nestedscrollview.java:1417)                                                                                     @ android.widget.framelayout.onmeasure(framelayout.java:436)                                                                                     @ android.support.v4.widget.nestedscrollview.onmeasure(nestedscrollview.java:482)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535)                                                                                     @ android.support.design.widget.coordinatorlayout.onmeasurechild(coordinatorlayout.java:713)                                                                                     @ android.support.design.widget.coordinatorlayout.onmeasure(coordinatorlayout.java:785)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535)                                                                                     @ android.widget.framelayout.onmeasure(framelayout.java:436)                                                                                     @ android.support.v7.widget.contentframelayout.onmeasure(contentframelayout.java:139)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535)                                                                                     @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436)                                                                                     @ android.widget.linearlayout.measurevertical(linearlayout.java:722)                                                                                     @ android.widget.linearlayout.onmeasure(linearlayout.java:613)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535)                                                                                     @ android.widget.framelayout.onmeasure(framelayout.java:436)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmargins(viewgroup.java:5535)                                                                                     @ android.widget.linearlayout.measurechildbeforelayout(linearlayout.java:1436)                                                                                     @ android.widget.linearlayout.measurevertical(linearlayout.java:722)                                                                                     @ android.widget.linearlayout.onmeasure(linearlayout.java:613)                                                                                     @ android.view.view.measure(view.java:17547)                                                                                     @ android.view.viewgroup.measurechildwithmarg 08-15 19:27:17.358 3080-3080/com.example.android.gatheraround i/process: sending signal. pid: 3080 sig: 9 

here code main activity , layouts if helps:

public class mainactivity extends appcompatactivity {      button eventlistbutton;     private bottomsheetbehavior mbottomsheetbehvior;     recyclerview rv;     linearlayoutmanager llm;     context context;     public list<events> events;      @override     protected void oncreate(bundle savedinstancestate) {           events = new arraylist<events>();          super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          //find button         eventlistbutton = (button) findviewbyid(r.id.eventlistbutton);          //find bottom sheet         android.support.v4.widget.nestedscrollview bottomsheet =                 (android.support.v4.widget.nestedscrollview) findviewbyid(r.id.bottomsheet);          //add bottomsheet behavior view         mbottomsheetbehvior = bottomsheetbehavior.from(bottomsheet);         mbottomsheetbehvior.sethideable(true);         mbottomsheetbehvior.setpeekheight(400);         mbottomsheetbehvior.setstate(bottomsheetbehavior.state_hidden);         eventlistbutton.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 if(mbottomsheetbehvior.getstate() == bottomsheetbehavior.state_expanded) {                     mbottomsheetbehvior.setstate(bottomsheetbehavior.state_collapsed);                     eventlistbutton.settext("hide");                 }                 else if(mbottomsheetbehvior.getstate() == bottomsheetbehavior.state_collapsed) {                     mbottomsheetbehvior.setstate(bottomsheetbehavior.state_hidden);                     eventlistbutton.settext("event list button");                 }                 else if(mbottomsheetbehvior.getstate() == bottomsheetbehavior.state_hidden) {                     mbottomsheetbehvior.setstate(bottomsheetbehavior.state_expanded);                     eventlistbutton.settext("peek");                 }             }         });          //bottomsheet callbacks         mbottomsheetbehvior.setbottomsheetcallback(new bottomsheetbehavior.bottomsheetcallback() {             @override             public void onstatechanged(@nonnull view bottomsheet, int newstate) {                 if (newstate == bottomsheetbehavior.state_expanded) {                     eventlistbutton.settext("peek");                 }                 else if (newstate == bottomsheetbehavior.state_collapsed) {                     eventlistbutton.settext("hide");                 }                 else if (newstate == bottomsheetbehavior.state_hidden) {                     eventlistbutton.settext("event list button");                 }             }              @override             public void onslide(@nonnull view bottomsheet, float slideoffset) {              }         });          //recyler view         rv = (recyclerview) findviewbyid(r.id.rv);         rv.sethasfixedsize(true);          llm = new linearlayoutmanager(context);          string[] participants = {"tamim","chiharu","azmain","miyoshi"};          events.add(new events(1302719286,"u22 project",participants,new latlng(37.652832,219.839478),"saizeriya"));         events.add(new events(1502419296,"u22 project",participants,new latlng(32.652832,19.839478),"hiroo"));         events.add(new events(1901719266,"u22 project",participants,new latlng(19.652832,39.839478),"canada"));         events.add(new events(1204219286,"u22 project",participants,new latlng(100.652832,13.839478),"tokyo monorail"));         rvadapter adapter = new rvadapter(events);         rv.setadapter(adapter);         rv.setlayoutmanager(new linearlayoutmanager(context));     } } 

main activity

<android.support.design.widget.coordinatorlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/bglayout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="0dp"     android:paddingleft="0dp"     android:paddingright="0dp"     android:paddingtop="0dp"     tools:context=".mainactivity">     <scrollview         android:layout_width="match_parent"         android:layout_height="match_parent"         app:layout_behavior="@string/appbar_scrolling_view_behavior"         >         <relativelayout             android:layout_width="match_parent"             android:layout_height="match_parent"             >                 <button                     android:layout_width="match_parent"                     android:layout_height="wrap_content"                     android:layout_margin="8dp"                     android:text="eventlist button"                     android:id="@+id/eventlistbutton"                     />          </relativelayout>     </scrollview>      <android.support.v4.widget.nestedscrollview         android:id="@+id/bottomsheet"         android:layout_width="match_parent"         android:layout_height="match_parent"         android:cliptopadding="true"         android:background="@color/recyclerview_background"         app:layout_behavior="@string/bottom_sheet_behavior"         >         <linearlayout             android:layout_width="match_parent"             android:layout_height="match_parent">             <android.support.v7.widget.recyclerview                 android:layout_height="match_parent"                 android:layout_width="match_parent"                 android:id="@+id/rv"                 android:layout_marginleft="@dimen/recyclerviewhorizontalpadding"                 android:layout_marginright="@dimen/recyclerviewhorizontalpadding"                 >              </android.support.v7.widget.recyclerview>          </linearlayout>      </android.support.v4.widget.nestedscrollview>  </android.support.design.widget.coordinatorlayout> 

and cardview:

<android.support.v7.widget.cardview     xmlns:card_view="http://schemas.android.com/apk/res-auto"     android:layout_width="match_parent"     android:layout_height="wrap_content"     xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/cardviewitem"     card_view:cardcornerradius="@dimen/cardviewcorner"     card_view:cardelevation="8dp"     card_view:cardbackgroundcolor="@color/cardbackground2"     android:layout_margintop="16dp"      >     <linearlayout         android:orientation="vertical" android:layout_width="match_parent"         android:layout_height="wrap_content"         >         <linearlayout             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:orientation="horizontal"             android:background="@color/cardbackground"             >             <textview                 android:layout_width="0dp"                 android:layout_height="match_parent"                 android:layout_weight="2"                 tools:text="dick museum tour"                 android:textsize="18sp"                 android:padding="@dimen/cardmargin"                 android:textstyle="bold"                 android:textcolor="@color/white"                 android:id="@+id/event_name"                 />             <textview                 android:layout_height="match_parent"                 android:layout_width="0dp"                 android:layout_weight="1"                 android:textsize="18dp"                 android:padding="@dimen/cardmargin"                 android:textstyle="bold"                 android:textcolor="@color/white"                 tools:text="8/24"                 android:textalignment="viewend"                 android:id="@+id/event_date"                 />          </linearlayout>         <linearlayout             android:layout_width="match_parent"             android:layout_height="@dimen/card_view_height"             android:background="@color/cardbackground2"             android:orientation="vertical"             >             <textview                 android:layout_width="match_parent"                 android:layout_height="0dp"                 android:layout_weight="1"                 tools:text="time:"                 android:textcolor="@color/white"                 android:textsize="18sp"                 android:gravity="center_vertical"                 android:paddingleft="@dimen/cardmargin"                 android:id="@+id/event_time"                 />             <textview                 android:layout_width="match_parent"                 android:layout_height="0dp"                 android:layout_weight="1"                 tools:text="location:"                 android:textcolor="@color/white"                 android:textsize="18sp"                 android:gravity="center_vertical"                 android:paddingleft="@dimen/cardmargin"                 android:clickable="true"                 android:id="@+id/event_location"                 />             <horizontalscrollview                 android:layout_width="match_parent"                 android:layout_height="0dp"                 android:layout_weight="1"                 >                 <!--todo(3):update view linear layout-->                 <linearlayout                     android:layout_width="match_parent"                     android:layout_height="match_parent"                     android:id="@+id/event_participants"                     android:orientation="horizontal"                     >                 </linearlayout>             </horizontalscrollview>         </linearlayout>      </linearlayout> </android.support.v7.widget.cardview> 

please note have tried version of code use textview instead of linearlayout concatenated names 1 whole string, , worked fine.

i trying create textview every individual name , add linear layout.

this happen because never set context inside adapter. add constructor inside adapter , set context variable.

public class rvadapter extends recyclerview.adapter<rvadapter.eventviewholder> {      context context;      public rvadapter(context context,...){          this.context = context     } }   rvadapter adapter = new rvadapter(this,events); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -