android - How to save changes in RecyclerView -
i'm new in android development don't have basics yet.
i'm stuck problem since few days.
problem follows:
got recyclerview items inside. created button add new item in list. adding function works fine, can see new item, if swipe third fragment (i got 3 fragments in app, , recyclerview on left one) or if kill app , relaunch it, every new items destroyed.
don't know how save kind of thing in android app. need show me way.
here code:
fragment recyclerview
public class camerafragment extends fragment { //matchselector reclycler view private arraylist<item> items = new arraylist<>(); private recyclerview recyclerview; private snaprecycleradapter adapter; // add private view addmatch; private alertdialog.builder alertdialog; private edittext team1_add; private view view; private boolean add = false; private int edit_position; private item newitem; public static camerafragment create() { return new camerafragment(); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_camera, container, false); initdialog(); return view; } @override public void onviewcreated(view view, bundle savedinstancestate) { //match selector vertical recycler view recyclerview = (recyclerview) view.findviewbyid(r.id.recycler_view); addmatch = (view) view.findviewbyid(r.id.add_match); /** * center snapping */ snaphelper snaphelper = new linearsnaphelper(); snaphelper.attachtorecyclerview(recyclerview); final customlinearlayoutmanager mcustomlinearlayoutmanager = new customlinearlayoutmanager(getcontext(), linearlayoutmanager.vertical, false); recyclerview.setlayoutmanager(mcustomlinearlayoutmanager); recyclerview.sethasfixedsize(true); adapter = new snaprecycleradapter(getcontext(), items); recyclerview.setadapter(adapter); matchlist(); adapter.notifydatasetchanged(); //button addmatch click addmatch.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { switch (v.getid()){ case r.id.add_match: removeview(); add = true; alertdialog.settitle("ajout"); team1_add.settext(""); alertdialog.show(); break; } } }); } // dialog ajout private void initdialog(){ alertdialog = new alertdialog.builder(getcontext()); view = getactivity().getlayoutinflater().inflate(r.layout.dialog_layout_addmatch,null); alertdialog.setview(view); alertdialog.setpositivebutton("ajouter", new dialoginterface.onclicklistener() { @override public void onclick(dialoginterface dialog, int which) { if(add){ add =false; newitem = new item(5, team1_add.gettext().tostring(), "0", "0","0" , "0" , r.mipmap.ic_blasonvierge, r.mipmap.ic_blasonvierge); adapter.additem(newitem); dialog.dismiss(); } else { items.set(edit_position, newitem); adapter.notifydatasetchanged(); dialog.dismiss(); } } }); team1_add = (edittext)view.findviewbyid(r.id.team1_add); } private void removeview(){ if(view.getparent()!=null) { ((viewgroup) view.getparent()).removeview(view); } } public void matchlist() { items.add(new item(1, "a", "b", "c","d" , "12:30" , r.mipmap.ic_blasonvierge, r.mipmap.ic_blasonvierge)); items.add(new item(4, "a", "b", "c","d" , "12:30" , r.mipmap.ic_blasonvierge, r.mipmap.ic_blasonvierge)); items.add(new item(2, "a", "b", "c", "d" , "12:30" , r.mipmap.ic_blasonvierge, r.mipmap.ic_blasonvierge)); items.add(new item(3, "a", "b", "c","d" , "12:30" , r.mipmap.ic_blasonvierge, r.mipmap.ic_blasonvierge)); adapter.notifydatasetchanged(); } }
snaprecycleradapter:
public class snaprecycleradapter extends recyclerview.adapter<snaprecycleradapter.recyclerviewholder> { private layoutinflater layoutinflater; private context context; private arraylist<item> items; public snaprecycleradapter(context context, arraylist<item> items) { this.layoutinflater = layoutinflater.from(context); this.context = context; this.items = items; } @override public recyclerviewholder oncreateviewholder(viewgroup parent, int viewtype) { view item = layoutinflater.inflate(r.layout.item_recycler_view, parent, false); return new recyclerviewholder(item); } @override public void onbindviewholder(final recyclerviewholder holder, int position) { item item = items.get(position); holder.logoteam1.setimageresource(item.getlogoteam1()); holder.logoteam2.setimageresource(item.getlogoteam2()); holder.team1.settext(item.getteam1()); holder.team2.settext(item.getteam2()); holder.categorie.settext(item.getcategorie()); holder.location.settext(item.getlocation()); holder.date.settext(item.getdate()); } public void additem(item newitem) { items.add(newitem); notifyiteminserted(items.size()); notifydatasetchanged(); } public void removeitem(int position) { items.remove(position); notifyitemremoved(position); notifyitemrangechanged(position, items.size()); } @override public int getitemcount() { return items.size(); } class recyclerviewholder extends recyclerview.viewholder { private imageview logoteam1; private imageview logoteam2; private textview team1; private textview team2; private textview categorie; private textview location; private textview date; private recyclerviewholder(final view v) { super(v); logoteam1 = (imageview) v.findviewbyid(r.id.logoteam1); logoteam2 = (imageview) v.findviewbyid(r.id.logoteam2); team1 = (textview) v.findviewbyid(r.id.team1); team2 = (textview) v.findviewbyid(r.id.team2); categorie = (textview) v.findviewbyid(r.id.categorie); location = (textview) v.findviewbyid(r.id.location); date = (textview) v.findviewbyid(r.id.date); } } }
objects list in recycleview created whenever specific fragment loaded. everytime kill application or move third fragment , first fragment reloaded , object created again containing default values have put. adding list valid till object remains in memory , else destroyed. based on requirement can go storage medium such sharedpreferences(using serailization) or sqlite(though not required small tasks) .
Comments
Post a Comment