0

我想通过sharepreferance将字符串存储在内存中,但是有一些问题无法存储在内存中,请告诉我如何解决这个问题?需要任何许可吗?

代码::

SavePreferences("MEM1", LanguageSelcted); 
LoadPreferences();

方法::

public void SavePreferences(String key, String value) {
            try {
                SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putString(key, value.trim());
                editor.commit();
            } catch (Exception e) {

            e.printStackTrace();

            }

private void LoadPreferences() {
        try {
            SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
            LanguageSelection = sharedPreferences.getString("MEM1", "");
            Log.i(TAG, "LanguageSelection is :>>>" + LanguageSelection);
            Toast.makeText(getBaseContext(), "LanguageSelection is :>>>" + LanguageSelection, Toast.LENGTH_LONG).show();
            /*
             * textSavedMem1.setText(strSavedMem1);
             * textSavedMem2.setText(strSavedMem2);
             */
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
4

2 回答 2

2

保存应该是

SharedPreferences myPrefs1 = this.getSharedPreferences(
                                "update", MODE_PRIVATE);
                        SharedPreferences.Editor prefsEditor = myPrefs1.edit();
                        prefsEditor.putString("URL", apkurl);
                        prefsEditor.commit();

得到应该是

SharedPreferences pref = this.getSharedPreferences("update",
                MODE_PRIVATE);
        String key = pref.getString("URL", "");

getSharedPreferences 将采用两个参数。

于 2012-05-24T13:39:18.927 回答
0

突出的一件事是这一行:

LanguageSelection = sharedPreferences.getString("MEM1", "");

应该有参考...

LanguageSelection whateverNameYouWant = sharedPreferences.getString("MEM1", "");

或者你的意思是……

String LanguageSelection = sharedPreferences.getString("MEM1", "");
于 2012-05-24T13:35:05.443 回答