How to get value of SharedPreferences android

前端 未结 5 1888
我在风中等你
我在风中等你 2021-01-23 02:41

I\'m trying to use SharedPreferences here is what i do

public void StoreToshared(Object userData){
    SharedPreferences mPrefs = getPreferences(MOD         


        
相关标签:
5条回答
  • 2021-01-23 03:15

    Try to add a key on your SharedPreferences:

    public void StoreToshared(Object userData){
        SharedPreferences mPrefs = getSharedPreferences("your_sp_key", MODE_PRIVATE); //add key
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
    
        Gson gson = new Gson();
        String json = gson.toJson(userData);
        Log.d("data", " Setup --> "+json);
        prefsEditor.putString("userinfo", json);
        prefsEditor.commit();
    
    }
    

    Retrieval:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        SharedPreferences mPrefs = getSharedPreferences("your_sp_key", MODE_PRIVATE); //add key
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
    
        String data = mPrefs.getString("userinfo", null);
        Log.i("Text", "Here is the retrieve");
        Log.i("data", " retrieve --> "+data);
    
    }
    
    0 讨论(0)
  • 2021-01-23 03:16

    You need to convert the string data from SharedPreferences back to a PoJo using Gson. Simply do this:

    Object userData = new Gson().fromJson(data, Object.class);
    

    I guess that should solve it.

    0 讨论(0)
  • 2021-01-23 03:21

    The api: getPreferences will use the activity name to create an xml file if not exists. For example, assume StoreToshared method is put in activity: LoginActivity.java, it will create a file: LoginActivity.xml to store your pref data. Hence, when you go into other activity, let say its name is: MainActivity.java, getPreferences will look into file "MainActivity.xml" instead of "LoginActivity.xml", that is why you cannot retrieve your data.

    The solution is to use: getSharedPreferences. Hence your code can be modified as follow:

    public void StoreToshared(Object userData) {

    SharedPreferences mPrefs = getSharedPreferences("FILE_NAME",MODE_PRIVATE);
    SharedPreferences.Editor prefsEditor = mPrefs.edit();
    
    Gson gson = new Gson();
    String json = gson.toJson(userData);
    Log.d("data", " Setup --> "+json);
    prefsEditor.putString("userinfo", json);
    prefsEditor.commit();
    

    }

    @Override protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        SharedPreferences mPrefs = getSharedPreferences("FILE_NAME",MODE_PRIVATE);
        SharedPreferences.Editor prefsEditor = mPrefs.edit();
    
        String data = mPrefs.getString("userinfo", null);
        Log.i("Text", "Here is the retrieve");
        Log.i("data", " retrieve --> "+data);
    
    }
    

    Hope this help.

    0 讨论(0)
  • 2021-01-23 03:30

    You can create 2 method:

    // put value
    public static void putPref(String key, String value, Context context) {
        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString(key, value);
        editor.commit();
    }
    

    and

    // get value
    public static String getPref(String key, Context context) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        return preferences.getString(key, null);
    }
    

    then you can put value

    putPref("userinfo", "/** user data json */", getApplicationContext());
    

    and get value

    String data = getPref("userinfo", getApplicationContext());
    

    I hope it can help your problem!

    0 讨论(0)
  • 2021-01-23 03:33

    In year 2020, Google has been released new data storage that is repleced of Shared Preference... It' is developed with "Kotlin"

    Source

    0 讨论(0)
提交回复
热议问题