java - Saving data persistently -
i have notes app whereby implemented 2 types of views: list , grid views. user can switch between listview
, gridview
depending on choice. issue have have been trying save state of view persistently such selected view opened @ start up. trying use sharedpreferences
achieve this. doing getting wrong in code?
private static final string key_name = "viewstate"; private listview mlistnotes; private gridview mgridnotes; sharedpreferences sharedpreferences; sharedpreferences.editor editor; private boolean mviewischanged = false; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); // set layouts list/grid mlistnotes = (listview) findviewbyid(r.id.main_listview); mgridnotes = (gridview) findviewbyid(r.id.main_gridview); // retrieve value shared preferences. sharedpreferences = getpreferences(context.mode_private); mviewischanged = sharedpreferences.getboolean(key_name, false); if (!mviewischanged){ mlistnotes.setvisibility(view.visible); mgridnotes.setvisibility(view.gone);} else { mlistnotes.setvisibility(view.gone); mgridnotes.setvisibility(view.visible); } @override public boolean onoptionsitemselected(menuitem item) { layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); view customtitleview = inflater.inflate(r.layout.dialog_menu, null); linearlayout mlistviewselect = (linearlayout) customtitleview.findviewbyid(r.id.list_select); linearlayout mgridviewselect = (linearlayout) customtitleview.findviewbyid(r.id.grid_select); switch (item.getitemid()) { case r.id.additem: // start noteactivity startactivity(new intent(this, noteactivity.class)); break; case r.id.changeview: final alertdialog alertbox = new alertdialog.builder(this).create(); alertbox.setcancelable(true); alertbox.setview(customtitleview); alertbox.show(); mlistviewselect.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // saving data sharedpreferences = getpreferences(context.mode_private); editor = sharedpreferences.edit(); editor.putboolean(key_name, mviewischanged); editor.apply(); mlistnotes.setvisibility(view.visible); mgridnotes.setvisibility(view.gone); alertbox.dismiss(); } }); mgridviewselect.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { // saving data in sharedpreferences mviewischanged = true; sharedpreferences = getpreferences(context.mode_private); editor = sharedpreferences.edit(); editor.putboolean(key_name, mviewischanged); editor.apply(); mlistnotes.setvisibility(view.gone); mgridnotes.setvisibility(view.visible); alertbox.dismiss(); } });
after in oncreate ()
mviewischanged = sharedpreferences.getboolean(key_name, false); mlistnotes = (listview) findviewbyid(r.id.main_listview); mgridnotes = (gridview) findviewbyid(r.id.main_gridview);
add
if (!mviewischanged){ mlistnotes.setvisibility(view.visible); mgridnotes.setvisibility(view.gone);} else { mlistnotes.setvisibility(view.gone); mgridnotes.setvisibility(view.visible); }
or put logic provided me after setting adapter in onresume() hope helps.
Comments
Post a Comment