[Android] uses JSON and sharedpreferences to store user-defined classes
Source: Internet
Author: User
<span id="Label3"></p><p><p>First look at Google's official definition of sharedpreferences:</p></p><p><p>The <code>SharedPreferences</code> class provides a general framework this allows you to save and retrieve persistent key-value pairs of primitive Data Types. You can use the <code>SharedPreferences</code> save any primitive data:booleans, floats, ints, longs, and Strings. This data would persist across user sessions (even if your application is killed).</p></p><p><p>Sharedpreferences can be used to store data permanently, even if the application is Destroyed. In fact, the data is stored on Android's internal memory.</p></p><p><p>There are two ways to get a sharedpreferences object. Getsharedpreferences ()-this method can be called when the application requires more than one name-sensitive storage file. Getpreferences ()-this method is called when the application requires only one storage File.</p></p><p><p>To write data in the sharedpreferences object, you need to:</p></p><p><p>1. Call the edit () method to get a sharedpreferences.editor object.</p></p><p><p>2. Use the editor Object's methods such as Putboolean () and putstring () to write the data to Editor.</p></p><p><p>3. Use the editor Object's method commit () or apply () to submit the Data.</p></p><p><p>Reading the data in sharedpreferences requires only calling sharedpreferences methods such as Getboolean () and GetString ().</p></p><p><p>Here is an example of using Sharedpreferences to store Data.</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Calc<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String Prefs_name = "myprefsfile"<span style="color: #000000;"><span style="color: #000000;">; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle State) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (state); . . . </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Restore Preferences</span></span>Sharedpreferences settings = getsharedpreferences (prefs_name, 0<span style="color: #000000;"><span style="color: #000000;">); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>Silent = Settings.getboolean ("silentmode",<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">); Setsilent (silent); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onStop () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnStop (); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">We need an Editor object to make preference Changes. </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">All objects is from <span style="color: #008000;">android.context.Context</span></span>Sharedpreferences settings = getsharedpreferences (prefs_name, 0<span style="color: #000000;"><span style="color: #000000;">); Sharedpreferences.editor Editor</span></span>=<span style="color: #000000;"><span style="color: #000000;">Settings.edit (); Editor.putboolean (</span></span>"silentmode"<span style="color: #000000;"><span style="color: #000000;">, msilentmode); </span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Commit the edits!</span></span><span style="color: #000000;"><span style="color: #000000;">Editor.commit (); }}</span></span></pre><p><p></p></p><p><p>As you can see from Google's introduction, the limitation of sharedpreferences is that it can only store basic types: boolean,floot,short,int,long,string and so On. For user-defined types, It is usually composed of multiple base type data and Arrays. such as a custom type:</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #000000;">action { </span><span style="color: #008000;">//</span> <span style="color: #008000;"> action Data</span> <span style="color: #0000ff;">private</span> <span style="color: #000000;"> UUID id; </span> <span style="color: #0000ff;">Private</span> <span style="color: #000000;">Date start;</span> <span style="color: #0000ff;">Private</span> <span style="color: #000000;">String actionname; </span> <span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">int</span> alarminghour=0<span style="color: #000000;">;</span> <span style="color: #0000ff;">Private</span> <span style="color: #0000ff;">New</span> <span style="color: #000000;">string[100];}</span></pre></pre><p><p>You cannot use Sharedpreferences to store it at this Time.</p></p><p><p>This time you need another protagonist: JSON.</p></p><p><p>The full name of JSON is the JavaScript object notation, which is the syntax for storing and exchanging textual information, similar to XML, which enables the transformation between object and Text.</p></p><p><p>Jsonobject and Jsonarray are built into ANDROID.</p></p><p><p>For such a user-defined type, you can</p></p><pre> <span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Transform the Action instance to Jsonobject,and then save jsonobject.tostring () * to Sharedpreference. * </span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;">the <span style="color: #008000;">Jsonobject corresponding to the Action *</span></span><span style="color: #808080;"><span style="color: #808080;">@throws</span></span><span style="color: #008000;"><span style="color: #008000;">jsonexception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span>Jsonobject ToJSON ()<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonexception{jsonobject JSON</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonobject (); Json.put (json_id,id.tostring ()); Json.put (json_startdate, Start.gettime ()); Jsonarray Array</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonarray (); Jsonobject jsonrecords[]</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">jsonobject[length]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; i<length;i++<span style="color: #000000;"><span style="color: #000000;">) {jsonrecords[i]</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonobject (); Jsonrecords[i].put (json_record, Records[i].getrecord ()); Array.put (i,jsonrecords[i]); } json.put (json_dailyrecords,array); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">json; }</span></span><span style="color: #008000;"><span style="color: #008000;">/**</span></span><span style="color: #008000;"><span style="color: #008000;">* Get the Action from Jsonobject *</span></span><span style="color: #808080;"><span style="color: #808080;">@param</span></span><span style="color: #008000;"><span style="color: #008000;">jsonstring the jsonstring retrieved from Sharedpreference. * </span></span><span style="color: #808080;"><span style="color: #808080;">@return</span></span><span style="color: #008000;">the <span style="color: #008000;">Action stored in Jsonobject *</span></span><span style="color: #808080;"><span style="color: #808080;">@throws</span></span><span style="color: #008000;"><span style="color: #008000;">jsonexception</span></span><span style="color: #008000;"><span style="color: #008000;">*/</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span>Action parsejsonstring (String Jsonstring)<span style="color: #0000ff;"><span style="color: #0000ff;">throws</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonexception{jsonobject json; JSON</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Jsonobject (jsonstring); UUID ID</span></span>=<span style="color: #000000;"><span style="color: #000000;">uuid.fromstring (json.getstring (json_id)); Date Start</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Date (json.getlong (json_startdate)); String ActionName</span></span>=<span style="color: #000000;"><span style="color: #000000;">json.getstring (json_name); string[] Dailyrecords</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span>dailyrecord[100<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></span> for</span>(<span style="color: #0000ff;"><span style="color: #0000ff;">int</span></span>i = 0; i<100;i++<span style="color: #000000;"><span style="color: #000000;">) {jsonobject Json_record</span></span>=<span style="color: #000000;"><span style="color: #000000;">Array.getjsonobject (i); String Record</span></span>=<span style="color: #000000;"><span style="color: #000000;">json_record.getstring (json_record); dailyrecords[i]</span></span>=<span style="color: #000000;"><span style="color: #000000;">record; } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Action (id,start,actionname,dailyrecords); }</span></span></pre><p><p>To achieve the conversion between action and jsonobject, Jsonobject is actually a form of text that can be easily converted to a string, therefore, you can store the action by using Sharedpreferences to store the converted String.</p></p><pre><pre>Sharedpreferences sp =<span style="color: #000000;"></span>=<span style="color: #000000;"> sp.edit ();</span> <span style="color: #0000ff;">Try</span> <span style="color: #000000;">{ </span><span style="color: #0000ff;">catch</span> <span style="color: #000000;"> (jsonexception e) { e.printstacktrace ();} Editor.apply ();</span></pre></pre><p><p>You can do this by reading the action from the Sharedpreferences:</p></p><pre><pre><span style="color: #0000ff;">Try</span> <span style="color: #000000;">{ </span>=<span style="color: #000000;"> getsharedpreferences (config.currentaction, context.mode_private); </span> = sp.getstring (config.currentactiondetails, ""<span style="color: #000000;">); </span> =<span style="color: #000000;"> action.parsejsonstring (jsonstring); </span> <span style="color: #0000ff;">Catch</span> <span style="color: #000000;">(jsonexception e) { e.printstacktrace (); }</span></pre></pre><p><p>This enables local storage and reading of the User-defined class Action.</p></p><p><p>[Android] uses JSON and sharedpreferences to store user-defined classes</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service