android - SharedPreferences Not retrieving JsonArray, JsonObject after device reboot -
i have set jsonarray , jsonobject application class make objects global. values stored in json array retrieve before device reboot. after reboot json array show blank squre '[]' bracket. out reboot shows me value of json array. after reboot doesn't. suggestions or guide can this.
this application class
public class myapplicationclass extends application{ public static final string prefs_name = "prefsfile"; private jsonarray jsonarray=new jsonarray(); private jsonobject jsonobj=new jsonobject(); public jsonarray getjsonarray() { return jsonarray; } public void setjsonarray(jsonarray jsonarray) { this.jsonarray = jsonarray; } public jsonobject getjsonobj() { return jsonobj; } public void setjjsonobj(jsonobject jsonobj) { this.jsonobj = jsonobj; } }
here setting jsonarray & jsonobject
// jsonobj, jsonarray declare class variables //getting global jsonarray object final myapplicationclass appobj=(myapplicationclass )getapplicationcontext(); final jsonarray jsonarray=appobj.getjsonalarmarray(); // getting name of sharedpreferences file & mode sharedprefs=getsharedpreferences(myapplicationclass.prefs_name, context.mode_private); final jsonobject jsonobj =appobj.getjsonobj(); btn.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub if(some condition) { jsonobj.put("value1", 1234); jsonobj.put("value2", 234); jsonobj.put("value3", 3); } else if(some condition)) { jsonobj.put("value1", 12314); jsonobj.put("value2", 2314); jsonobj.put("value3", 31); } jsonarray.put(jsonobj); appobj.setjsonalarmarray(jsonarray); editor= sharedprefs.edit(); editor.putstring("key", jsonarray.tostring()); system.out.println(jsonarray.tostring()); editor.commit(); } }); this way retrieving json array
btn2.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub sharedprefs=getsharedpreferences(myapplicationclass.prefs_name, 0); try { jsonarray jsonarray=((myapplicationclass)getapplication()).getjsonalarmarray(); system.out.println(jsonarray.tostring()); toast.maketext(typesactivity.this, ""+jsonarray, toast.length_long).show(); } catch (exception e) { e.printstacktrace(); } } });
you forgot read json sharedpreferences:
btn2.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { // todo auto-generated method stub sharedprefs=getsharedpreferences(myapplicationclass.prefs_name, 0); string jsonarraystring = sharedprefs.getstring("key", null); try { if (jsonarraystring != null) { jsonarray jsonarray= new jsonarray(jsonarraystring) system.out.println(jsonarray.tostring()); toast.maketext(typesactivity.this, ""+jsonarray, toast.length_long).show(); } } catch (exception e) { e.printstacktrace(); } } });
Comments
Post a Comment