Advanced Shared Preference Usage

by Friday, September 27, 2013 0 comments

Shared Preferences to store private primitive data in key-value pairs. From this example you can store string, boolean, Arraylist in private.


 import java.lang.reflect.Type;  
 import java.util.ArrayList;  
 import java.util.HashMap;  
 import java.util.List;  
 import org.apache.http.entity.mime.content.ContentBody;  
 import android.content.Context;  
 import android.content.SharedPreferences;  
 import com.google.gson.Gson;  
 import com.google.gson.GsonBuilder;  
 import com.google.gson.reflect.TypeToken;  
 public class Sharedpref {  
      public static void writeList(Context context, List<String> list, String prefix)  
      {  
           String appname=context.getResources().getString(R.string.app_name)+"Arraylist";  
        SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = prefs.edit();  
        int size = prefs.getInt(prefix+"_size", 0);  
        // clear the previous data if exists  
        for(int i=0; i<size; i++)  
          editor.remove(prefix+"_"+i);  
        // write the current list  
        for(int i=0; i<list.size(); i++)  
          editor.putString(prefix+"_"+i, list.get(i));  
        editor.putInt(prefix+"_size", list.size());  
        editor.commit();  
      }  
      public static List<String> readList (Context context, String prefix)  
      {  
           String appname=context.getResources().getString(R.string.app_name)+"Arraylist";  
        SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        int size = prefs.getInt(prefix+"_size", 0);  
        List<String> data = new ArrayList<String>(size);  
        for(int i=0; i<size; i++)  
          data.add(prefs.getString(prefix+"_"+i, null));  
        return data;  
      }  
      public static void setPrefBoolean(Context context, String key, boolean value) {  
           String appname = context.getResources().getString(R.string.app_name)  
                     + "String";  
           SharedPreferences prefs = context.getSharedPreferences(appname,  
                     Context.MODE_PRIVATE);  
           SharedPreferences.Editor editor = prefs.edit();  
           editor.putBoolean(key, value);  
           editor.commit();  
      }  
      public static boolean getPrefBoolean(Context context, String key) {  
           String appname = context.getResources().getString(R.string.app_name)  
                     + "String";  
           SharedPreferences prefs = context.getSharedPreferences(appname,  
                     Context.MODE_PRIVATE);  
           boolean prefstr = prefs.getBoolean(key, false);  
           return prefstr;  
      }  
      public static void SetPrefString(Context context,String key, String value){  
           String appname=context.getResources().getString(R.string.app_name)+"String";  
        SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        SharedPreferences.Editor editor = prefs.edit();  
        editor.putString(key, value);  
        editor.commit();  
        }  
      public static String GetPrefString(Context context,String key){  
           String appname=context.getResources().getString(R.string.app_name)+"String";  
             SharedPreferences prefs = context.getSharedPreferences(appname, Context.MODE_PRIVATE);  
        String prefstr = prefs.getString(key, "");  
        return prefstr;  
        }  
 }  

Unknown

Androider

Welcome to Android-Action Blog. I’m a normal guy, who is passionate about Mobile Coding. Here I am writing about Android. Happy learning

0 comments:

Post a Comment