Sharedpreferences storage complex object Solution

Source: Internet
Author: User
The android SDK for complex object storage service does not provide related APIs. If you want to use an XML node to represent complex objects, it will take a lot of time to parse them, so you can find a simple method, that is, you know how to store serialized objects in the string type with base64.
The following is the tested code used in my project:
/*** Sharedpreferences tool class ** @ author Bobby **/public class sharedpreferencesutils {context; string name; Public sharedpreferencesutils (context, string name) {This. context = context; this. name = Name ;} /*** obtain the value of the value based on the key and expected value types ** @ Param key * @ Param clazz * @ return */Public <t> T getvalue (string key, class <t> clazz) {If (context = NULL) {Throw new runtimeexception (" Please call the construction with the context and name parameters first! ");} Sharedpreferences sp = This. context. getsharedpreferences (this. name, context. mode_private); Return getvalue (Key, clazz, SP );} /*** for complex storage <Object> ** @ Param key * @ Param Val */Public void setobject (string key, object) {sharedpreferences sp = This. context. getsharedpreferences (this. name, context. mode_private); bytearrayoutputstream baos = new bytearrayoutputstream (); objectoutputstream Out = NULL; try {out = new objectoutputstream (baos); out. writeobject (object); string objectval = new string (base64.encode (baos. tobytearray (), base64.default); Editor editor = sp. edit (); Editor. putstring (Key, objectval); Editor. commit ();} catch (ioexception e) {e. printstacktrace ();} finally {try {If (baos! = NULL) {baos. Close ();} If (OUT! = NULL) {out. close () ;}} catch (ioexception e) {e. printstacktrace () ;}}@ suppresswarnings ("unchecked") Public <t> T GetObject (string key, class <t> clazz) {sharedpreferences sp = This. context. getsharedpreferences (this. name, context. mode_private); If (sp. contains (key) {string objectval = sp. getstring (Key, null); byte [] buffer = base64.decode (objectval, base64.default); bytearrayinputstream B AIS = new bytearrayinputstream (buffer); objectinputstream OIS = NULL; try {OIS = new objectinputstream (BAIS); t = (t) Ois. readobject (); Return t;} catch (streamcompuptedexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();} catch (classnotfoundexception e) {e. printstacktrace ();} finally {try {If (BAIS! = NULL) {BAIS. Close ();} If (OIS! = NULL) {Ois. close () ;}} catch (ioexception e) {e. printstacktrace () ;}} return NULL ;} /*** transition method for external visibility ** @ Param key * @ Param clazz * @ Param sp * @ return */@ suppresswarnings ("unchecked ") private <t> T getvalue (string key, class <t> clazz, sharedpreferences SP) {T; try {T = clazz. newinstance (); If (T instanceof integer) {return (t) integer. valueof (sp. getint (Key, 0);} else if (T instanceof string) {return (t) sp. getstring (key, "");} else if (T instanceof Boolean) {return (t) Boolean. valueof (sp. getboolean (Key, false);} else if (T instanceof long) {return (t) Long. valueof (sp. getlong (Key, 0l);} else if (T instanceof float) {return (t) float. valueof (sp. getfloat (Key, 0l);} catch (instantiationexception e) {e. printstacktrace (); log. E ("system", "Type input error or complex types cannot be parsed [" + E. getmessage () + "]");} catch (illegalaccessexception e) {e. printstacktrace (); log. E ("system", "Type input error or complex types cannot be parsed [" + E. getmessage () + "]");} log. E ("system", "cannot find" + key + "corresponding value"); return NULL ;}}



Sharedpreferences storage complex object Solution

Related Article

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.