android - How to refresh RecyclerView from button on DialogFragment -


i have application recyclerview , dialogfragment, in dialog add data database , display in recyclerview. tried refresh recyclerview when clicked in add.

this fragment

public class addaction extends dialogfragment implements view.onclicklistener { edittext addtitle, adddesc; button add, clear,close; context context; private databasehelpher db; string title,des; public addaction() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) {     view rootview = inflater.inflate(r.layout.addaction, container, false);     addtitle = (edittext) rootview.findviewbyid(r.id.todotitle);     adddesc = (edittext) rootview.findviewbyid(r.id.tododescription);     add = (button) rootview.findviewbyid(r.id.addbutton);     add.setonclicklistener(this);     close = (button) rootview.findviewbyid(r.id.close);     close.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             dismiss();         }     });      clear = (button) rootview.findviewbyid(r.id.clear);     clear.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view view) {             addtitle.settext("");             adddesc.settext("");         }     });     return rootview; } public void onactivitycreated(@nullable bundle savedinstancestate) {     super.onactivitycreated(savedinstancestate);     getdialog().settitle("add action");     db = new databasehelpher(getcontext()); } private void insert() {     title = addtitle.gettext().tostring();     des= adddesc.gettext().tostring();     db.insertintodb(title, des); } @override public void onclick(view v) {     if (addtitle.gettext().tostring().trim().equals("")) {         addtitle.seterror(" title required!");     } else if (adddesc.gettext().tostring().trim().equals("")) {         adddesc.seterror(" postion required!");     }     insert(); } 

}

and mainactivity

public class mainactivity extends appcompatactivity {     list<todomodule> dblist;     recyclerview mrecyclerview;     databasehelpher helpher;     private recyclerview.adapter madapter;     private recyclerview.layoutmanager mlayoutmanager;     @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);         toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar);         setsupportactionbar(toolbar);         getsupportactionbar().hide();         helpher = new databasehelpher(this);         dblist= new arraylist<todomodule>();         dblist = helpher.getdatafromdb();         mrecyclerview = (recyclerview)findviewbyid(r.id.apprecyclerview);         mrecyclerview.sethasfixedsize(true);         mlayoutmanager = new linearlayoutmanager(this);         mrecyclerview.setlayoutmanager(mlayoutmanager);         madapter = new recycleradapter(this,dblist);         madapter.notifydatasetchanged();         mrecyclerview.setadapter(madapter);         floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab);         fab.setimageresource(r.drawable.ic_action_name);         fab.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 fragmentmanager fm = getsupportfragmentmanager();                 addaction add = new addaction();                 add.show(fm, "fragment_edit_name");             }         });}     @override     protected void onresume() {         super.onresume();         dblist = helpher.getdatafromdb();         madapter.notifydatasetchanged();     } } 

first solution

cast activity , call method this:

in activity add method

public void mymethod(){     runonuithread(new runnable() {         @override         public void run() { //your code here         }     }); } 

in dialogfragment call activity method after cast it.

 ((youractivity)getactivity()).mymethod(); 

second solution

add eventbus gradle

compile 'org.greenrobot:eventbus:3.0.0' 

make class want pass activity

public class messageevent {     public final string message;     public messageevent(string message) {         this.message = message;     } } 

listen events in activity

// method called when messageevent posted (in ui thread toast) @subscribe(threadmode = threadmode.main) public void onmessageevent(messageevent event) {     toast.maketext(getactivity(), event.message, toast.length_short).show(); } 

register eventbus in activity

@override public void onstart() {     super.onstart();     eventbus.getdefault().register(this); }  @override public void onstop() {     eventbus.getdefault().unregister(this);     super.onstop(); } 

finally send in event dialog activity

eventbus.getdefault().post(new messageevent("hello everyone!"));  

don't forgot adding proguard file

-keepattributes *annotation* -keepclassmembers class ** {     @org.greenrobot.eventbus.subscribe <methods>; } -keep enum org.greenrobot.eventbus.threadmode { *; } 

Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -