android - Filtering RecyclerView cards when NavigationView menu item selected -


i have activity design support navigationview implementation. drawer has 3 menu items : home, sort x, sort y. activity displays recyclerview cards shows data list. list has objects have string tag ("x" or "y"). need sort recyclerview tag when respective menu item selected.

myactivity activity:

i npe when call setfilter method home fragment.

public class myactivity extends appcompatactivity {  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     .     .     .     init(); }  private void init() { mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mnavigationview = (navigationview) findviewbyid(r.id.navigation);      mnavigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() {         @override         public boolean onnavigationitemselected(menuitem menuitem) {             home homefrag = new home();             mdrawerlayout.closedrawers();             switch (menuitem.getitemid()) {                 case r.id.nav_home:                     if(!menuitem.ischecked()) {                         setfragment(new home());                     }                     break;                 case r.id.nav_x:                     if(!menuitem.ischecked()) {                         homefrag.setfilter("x");                     }                     break;                 case r.id.nav_y:                     if(!menuitem.ischecked()) {                         homefrag.setfilter("y");                     }                     break;             }             menuitem.setchecked(true);             mtoolbar.settitle(menuitem.gettitle());             return true;         }     }); } 

home fragment:

i npe in setfilter() method @ filteredobjects.clear() , if remove npe @ loop.

public class home extends fragment {  private recyclerview mrecyclerview;  list<object> objects; list<object> filteredobjects;  @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view v = inflater.inflate(r.layout.fragment_home, container, false);     .     .     .     mrecyclerview = (recyclerview) v.findviewbyid(r.id.rv);     linearlayoutmanager llm = new linearlayoutmanager(v.getcontext());     mrecyclerview.setlayoutmanager(llm);      objects = new arraylist<>();     filteredobjects = new arraylist<>();      initializedata(objects);     initializeadapter(objects);     return v; }  public void setfilter(string querytext) {     querytext = querytext.tolowercase();      filteredobjects.clear();    // nullpointerexception here      (object item : objects) {         final string text = item.tag.tolowercase();         if (text.equals(querytext))             filteredobjects.add(item);     }     initializeadapter(filteredobjects); }  private void initializeadapter(list<object> list){     rvadapter adapter = new rvadapter(list);     adapter.notifydatasetchanged();     mrecyclerview.setadapter(adapter); }  private void initializedata(list<object> list){      list.add(new object("object 1", "x"));     list.add(new object("object 2", "y"));     list.add(new object("object 3", "x"));     list.add(new object("object 4", "x"));     list.add(new object("object 5", "y"));     list.add(new object("object 6", "y")); } 

logcat:

com.experiments.material e/androidruntime﹕ fatal exception: main     process: com.experiments.material, pid: 32273     java.lang.nullpointerexception             @ com.experiments.material.home.setfilter(home.java:170)             @ com.experiments.material.myactivity$2.onnavigationitemselected(myactivity.java:134)             @ android.support.design.widget.navigationview$1.onmenuitemselected(navigationview.java:136)             @ android.support.v7.internal.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:811)             @ android.support.v7.internal.view.menu.menuitemimpl.invoke(menuitemimpl.java:153)             @ android.support.v7.internal.view.menu.menubuilder.performitemaction(menubuilder.java:958)             @ android.support.design.internal.navigationmenupresenter.onitemclick(navigationmenupresenter.java:179)             @ android.widget.adapterview.performitemclick(adapterview.java:299)             @ android.widget.abslistview.performitemclick(abslistview.java:1152)             @ android.widget.abslistview$performclick.run(abslistview.java:3014)             @ android.widget.abslistview$3.run(abslistview.java:3865)             @ android.os.handler.handlecallback(handler.java:808)             @ android.os.handler.dispatchmessage(handler.java:103)             @ android.os.looper.loop(looper.java:193)             @ android.app.activitythread.main(activitythread.java:5299)             @ java.lang.reflect.method.invokenative(native method)             @ java.lang.reflect.method.invoke(method.java:515)             @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:825)             @ com.android.internal.os.zygoteinit.main(zygoteinit.java:641)             @ de.robv.android.xposed.xposedbridge.main(xposedbridge.java:132)             @ dalvik.system.nativestart.main(native method) 

edit: activity_main.xml

<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     android:id="@+id/drawer_layout"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:fitssystemwindows="true">      <linearlayout         android:orientation="vertical"         android:layout_width="match_parent"         android:layout_height="match_parent">         <android.support.design.widget.appbarlayout             android:layout_height="?attr/actionbarsize"             android:layout_width="match_parent" >              <include                 layout="@layout/toolbar"                 android:layout_width="match_parent"                 android:layout_height="wrap_content" />         </android.support.design.widget.appbarlayout>          <framelayout             android:name="com.experiments.material.home"             android:id="@+id/fragment_container"             android:layout_width="match_parent"             android:layout_height="match_parent" />      </linearlayout>      <android.support.design.widget.navigationview         android:id="@+id/navigation"         android:layout_width="wrap_content"         android:layout_height="match_parent"         android:layout_gravity="start"         app:menu="@menu/navigation_items"         app:headerlayout="@layout/header_layout" />  </android.support.v4.widget.drawerlayout> 

when post logcat, add caused by part too.

your fragment null, if it's not visible, setfilter won't work. try code:

in fragment:

private string msetfilterstring; public home (string filter) {     this.msetfilterstring = filter; } 

in oncreateview

if (msetfilterstring != null) {     setfilter(msetfilterstring); } else {     initializeadapter(objects); }     return v; 

instead of

initializeadapter(objects); return v; 

and in activity:

mnavigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() {     @override     public boolean onnavigationitemselected(menuitem menuitem) {         mdrawerlayout.closedrawers();         switch (menuitem.getitemid()) {             case r.id.nav_home:                 if(!menuitem.ischecked()) {                     getfragmentmanager().begintransaction().replace(r.id.fragment_container, (fragment) new home(null)).commit();                 }                 break;             case r.id.nav_x:                 if(!menuitem.ischecked()) {                     getfragmentmanager().begintransaction().replace(r.id.fragment_container, (fragment) new home("x")).commit();                 }                 break;             case r.id.nav_y:                 if(!menuitem.ischecked()) {                     getfragmentmanager().begintransaction().replace(r.id.fragment_container, (fragment) new home("y")).commit();                 }                 break;         }         menuitem.setchecked(true);         mtoolbar.settitle(menuitem.gettitle());         return true;     } }); 

you didn't initialize filteredobjects. have declarated it:

list<object> filteredobjects; 

you must initialize object. example:

list<object> filteredobjects= new arraylist<object>(); 

Comments

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -