[英]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.