簡體   English   中英

在活動之間使用SharedPreferences

[英]Using SharedPreferences between Activity

我有一個配置頁,上面有一個表格。 當按下按鈕時,我想將該值保存到SharedPreference。 然后,需要從我的應用程序中的其他位置訪問此SharedPreference值。

我正在嘗試保存如下所示的值。 我想保存collectionID,以便可以在其他地方使用

public class ConfigPage extends Activity {

    public static final String PREFS_NAME = "MyPrefsFile";

    Button   mButton;
    EditText mEdit;
    String collectionID;
    String key = "GregKey";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.config);
        getActionBar().hide();

        mButton = (Button)findViewById(R.id.setCollection);
        mEdit   = (EditText)findViewById(R.id.collectionName);

        mButton.setOnClickListener(
                new View.OnClickListener()
                {
                    public void onClick(View view)
                    {
                        collectionID = mEdit.getText().toString();
                        Log.d("EditText", collectionID);
                        SharedPreferences settings =
                                getSharedPreferences(PREFS_NAME, 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putString(key, collectionID);
                        editor.commit();
                    }
                });

    }
}

保存后,我需要在另一個類中訪問它,但是我不知道該怎么做。 上面的示例目前使應用程序崩潰,因此有些不對勁

我想保存collectionID,以便可以在其他地方使用

使用mButton Button onClick事件將SharedPreferences EditText文本保存為:

 mButton.setOnClickListener( new View.OnClickListener()
         {
            public void onClick(View view)
             {
                collectionID = mEdit.getText().toString();
                 Log.d("EditText", collectionID);
                 // save value here in SharedPreferences
                 SharedPreferences settings = 
                            ConfigPage.this.getSharedPreferences(PREFS_NAME, 0);
                 SharedPreferences.Editor editor = settings.edit();
                 editor.putString(collectionID, collectionID);
                 editor.commit();
               }
         });

您的崩潰是因為您的值為null

String savedID;

您需要為變量添加一個值:

String savedID = "somevalue";

此外,只要不按下Button您的鍵就為空 ,這也會導致崩潰。

putString(String key, String value)方法使您可以使用特定的key存儲特定的 ,以后可以使用該key重新訪問存儲的值。

例:

String key = "somekey";
String value = "yourvaluetostore";

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(key, value); // store the value
editor.commit();

在另一個活動中:

String key = "somekey";
SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);

// get the value "" is default
String value = sharedPreferences.getString(key, ""); 

您在其他活動中所需要的就是存儲值的鍵 使用此鍵,可以從SharedPreferences提取正確的存儲值。 -> 需要密鑰來標識值。

由於您正在使用代碼的一部分:

SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(collectionID, savedID);
editor.commit();

Log.d("Saved as", savedID);

onCreate方法中,

...
editor.putString(null, null);
Log.d("Saved as", null);

更正您的代碼,以便您之前填寫這些元素。 我想您想在OnClick部分進行保存

是的,這並不完全正確,因為我看到您沒有節省任何費用。 在共享首選項上,您應該有一個“鍵”,該鍵將用於標識示例中保存的值,它應該是這樣的

public class ConfigPage extends Activity {

public static final String PREFS_NAME = "MyPrefsFile";
public static final String COLLECTIONID_KEY = "COLLECTIONID_KEY";
Button   mButton;
EditText mEdit;
String collectionID;
String savedID;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.config);
    getActionBar().hide();

    mButton = (Button)findViewById(R.id.setCollection);
    mEdit   = (EditText)findViewById(R.id.collectionName);

    mButton.setOnClickListener(
            new View.OnClickListener()
            {
                public void onClick(View view)
                {
                    collectionID = mEdit.getText().toString();
                    Log.d("EditText", collectionID);
                }
            });

    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putString(COLLECTIONID_KEY, collectionID);
    editor.commit();

    Log.d("Saved as", COLLECTIONID_KEY);
}

}

然后檢索它:

SharedPreferences sharedPreferences = getSharedPreferences(PREFS_NAME, 0);
String mySavedCollectionID = sharedPreferences.getString(COLLECTIONID_KEY);

並確保其在Onclick事件上完成,否則您可能會再次崩潰,因為在代碼中的on-click事件之后,下面的行仍將運行並保存null!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM