Sharedpreferences Package Class

Source: Internet
Author: User

Today is free, the file configuration Sharepreference write a package of demo good tidy up.

First look at the definition of it on the Internet:

Sharedpreferences is the easiest data storage technology to understand in Android, and in fact sharedpreferences is dealing with a key-value (key-value pair) Sharedpreferences used to store some lightweight data.

Encapsulated class

package com.example.testlistq;import android.content.context;import  Android.content.sharedpreferences;public class sharedpreferencesutils {private static  final String FILE_NAME =  "Share_date";/** *  the method of saving data, we need to get the specific type of data to be saved, Then call different save methods based on the type  *  *  @param  context *  @param  key   Save the corresponding key  *  @param  object   Save the corresponding value  */public static void setparam (Context  context, string key, object object)  {String type =  Object.getclass (). Getsimplename (); Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private); Sharedpreferences.editor editor = sp.edit ();if  ("String". Equals (type))  { Editor.putstring (key,  (String)  object);}  else if  ("integer". Equals (Type))  {editor.putint (key,  (integer)  object);}  else if  ("Boolean". Equals (Type))  {editor.putboolean (key,  (Boolean)  object);}  else if  ("float". Equals (Type))  {editor.putfloat (key,  (float)  object);}  else if  ("Long". Equals (Type))  {editor.putlong (key,  (long)  object);} Editor.commit ();} /** *  get the method to save the data, we are based on the specific type of data that is worth saving, and then call the relative method to get the value  *  *  @param  context  *  @param  key *  @param  defaultObject *  @return  */public static  object getparam (Context context, string key, object defaultobject)  { String type = defaultobject.getclass (). Getsimplename (); Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private);   ("string". Equals (Type))  {return sp.getstring (key,  (string)  defaultobject);}  else if  ("Integer". Equals (Type))  {return sp.getint (Key,  (Integer)  defaultobject);}  else if  ("Boolean". Equals (Type))  {return sp.getboolean (key,  (Boolean)   Defaultobject);}  else if  ("float". Equals (Type))  {return sp.getfloat (key,  (float)   Defaultobject);}  else if  ("Long". Equals (Type))  {return sp.getlong (key,  (Long)  defaultobject );} Return null;}}



Invoke instance

Sharedpreferencesutils.setparam (This, "name", "Xiaoming"); Sharedpreferencesutils.setparam (this, "id", 10241); Sharedpreferencesutils.setparam (This, "Ifold", true); String str = Sharedpreferencesutils.getparam (this, "name", "name"). ToString (); String id = sharedpreferencesutils.getparam (this, "id", 123). ToString (); String old = Sharedpreferencesutils.getparam (this, "Ifold", false). ToString (); Toast.maketext (this, id + "," + str + "," + old, Toast.length_long). Show ();




This article from "Climb over the mountains to see the sea" blog, declined to reprint!

Sharedpreferences Package Class

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.