Anypref, a Sharedpreferences tool class that can save object instances directly in Sharedpreferences

Source: Internet
Author: User

Sometimes when writing code, there are often some need to persist an object, if you use SQLite and feel too heavy, using sharedpreferences to save is really lightweight, but also for the object of each field to be saved, it may be a lot of lines of code, Read out is a lot of lines of code, why we do not directly save and read the Object field, if there is a need to save a few different objects, it would save a lot of code.


Anypref is a sharedpreferences tool class that can save an object directly to Sharedpreferences, using the method:


Add Jitpack to the project root build.gradle:


Allprojects {repositories {maven {URL "Https://jitpack.io"}}}


In using App/build.gradle, add the following:


dependencies {compile ' com.github.nashlegend:anypref:1.2.1 '}


The basic principle of anypref is to use reflection to read the field name and to save the field name as a key in the sharedpreferences as the name of Sharedpreferences, It also supports the use of annotations to customize Sharedpreferences's name and key, which, by default, saves all public fields supported by the sharedpreferences (except for static and final adornments). You can also use annotations to exclude certain fields that you do not need. If the object to be saved also contains another complex sub-object, such as a field in the family class that has a son, the son object is not saved by default, and if you want to save the sub-object at the same time, you need to add the prefsub annotation. Similarly if you want to save an object that also contains a ArrayList, to save the ArrayList, you need to add prefarraylist annotations.


How do I save and read data?


Suppose there is a sample class.


     @PrefModel ("Prefname")//Do not add this annotation, "prefname" means to save the name of Sharedpreferences, can be any string strings, If not written, the full name of the class     public class Sample {              @PrefField ("Intfieldkey")//Do not add this annotation, "intfieldkey" means to save this value when the key, can be any string strings, If not written, the variable name for this variable         public int intField = 32;                  @PrefIgnore// Adding this note indicates that this variable is not saved         public float floatField =  1.2345f;                @ Preffield (numdef = 110)//Indicates if the default value used after reading is not available         public  long longfield = 95789465213l;                 public string stringfield =  "String";                  @PrefField (booldef = true)          public boolean boolField = false;                  @PrefField (value =  " Setvaluewithspecifiedkey ",  strdef = {" 1 ", " 2 ", " 3 ", " 4 "})//default value is [1,2,3,4]         public Set<String> setValue = new  Linkedhashset<> ();                   @PrefSub (nullable = false)//nullable Indicates whether a child object can be null when the child object is taken, and the default is true         public subsample son1;//marked the @prefsub variable, Although it is not a sharedpreferences supported type, it will still be saved &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&Nbsp;           @PrefArrayList (Nullable = true,  itemnullable = true)//nullable, itemnullable indicates whether the data in the list can be null, the default is true         public arraylist<subsample> samplearraylist;// ArrayList that are marked with @prefarraylist will be saved, but ArrayList cannot be a basic type of     }


Save data:


Anypref.put (sample); or anypref.put (sample, "Your Prefname"); the second parameter is the sharedpreferences name that you defined to save the class, not the Prefmodel definition of the name


Reading data


Sample sample = Anypref.get (Sample.class);    or Sample sample = Anypref.get (Sample.class, "your Prefname"); or Sample sample = Anypref.get (Sample.class, "Your Prefname", true);//The third parameter indicates whether the read-out object can be null, default is NOT NULL


Clear Data


Anypref.clear (Sample.class); or Anypref.clear (Sample.class, "your Prefname");


It's so simple ~


There are also several ways to simplify operations sharedpreferences read and write arbitrary data:


    anypref.getprefs ("sample")              .putlong ("Long",  920394857382l)              .putint ("int",  63)             . Putstring ("string",  "sample string");     anypref.getprefs (Sample.class)              .begintransaction ()              .putlong ("Long",  920394857382l)              .putint ("int",  63)              .putstring ("string",  "sample string")              .commit ();     sharedprefs sharedprefs  = anypref.getprEFS ("sample");     system.out.println (Sharedprefs.getint ("int",  0);     system.out.println (Sharedprefs.getlong ("Long",  0));     system.out.println ( Sharedprefs.getstring ("string",  ""));


Project GitHub Address Https://github.com/NashLegend/AnyPref




This article is from the "nashlegend" blog, make sure to keep this source http://nashlegend.blog.51cto.com/5635342/1788954

Anypref, a Sharedpreferences tool class that can save object instances directly in Sharedpreferences

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.