[英]Retrieving sharedpreferences string in another activity
I am trying to save a few edittext fields in one activity using sharedpreferences and then output those saved values to textview fields on another activity. 我试图使用sharedpreferences在一个活动中保存一些edittext字段,然后将那些保存的值输出到另一个活动的textview字段中。 The data appears to be saving ok in the first activity but I am not able to resolve the stored variable in the new activity.
数据似乎在第一个活动中保存正常,但是我无法在新活动中解析存储的变量。 Does anyone know what i'm doing wrong?
有人知道我在做什么错吗? Thanks!
谢谢!
public class RelativeLayout extends AppCompatActivity {
EditText edit1;
EditText edit2;
EditText edit3;
public String Name = "nameKey";
public String Email = "emailKey";
public String Phone = "phoneKey";
public static final String MyPREFERENCES = "PrefStore" ;
SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_relative_layout);
final Button ButtonReturn = (Button) findViewById(R.id.button3);
settings = getSharedPreferences(MyPREFERENCES, 0);
//Onclick listen for return button press. On press App returns to main activity.
ButtonReturn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
edit1=(EditText)findViewById(R.id.editText);
edit2=(EditText)findViewById(R.id.editText2);
edit3=(EditText)findViewById(R.id.editText3);
String n = edit1.getText().toString();
String e = edit2.getText().toString();
String p = edit3.getText().toString();
SharedPreferences.Editor editor = settings.edit();
editor.putString(Name, n);
editor.putString(Email, e);
editor.putString(Phone, p);
editor.apply();
The second activity is below. 第二项活动如下。 I am trying to retrieve the Name variable in the sharedpreferences but it appears in red with an error.
我正在尝试在sharedpreferences中检索Name变量,但是它显示为红色并带有错误。 Cannot resolve symbol 'Name'.
无法解析符号“名称”。
public class MainActivity extends AppCompatActivity {
SharedPreferences settings;
public static final String MyPREFERENCES = "PrefStore" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button ButtonDCUWeb = (Button) findViewById(R.id.ButtonDCUWeb);
final Button ButtonStartCamera = (Button) findViewById(R.id.ButtonStartCamera);
final Button ButtonLinearLayout = (Button) findViewById(R.id.ButtonLinearLayout);
final Button ButtonRelativeLayout = (Button) findViewById(R.id.ButtonRelativeLayout);
settings = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
String value = settings.getString(Name, "Default_Value");
Your variable Name
is only defined on the scope of your first activity. 您的变量
Name
仅在第一个活动的范围内定义。 You need to make it accessible in the second activity... You can do two things: 您需要在第二个活动中使其可访问...您可以做两件事:
1 - Declare a variable Name
in your second activity with the same value as the first. 1-在第二个活动中声明一个变量
Name
,该变量Name
与第一个活动具有相同的值。 2 - Make the Name
variable static so you can access it in the second activity: public static String Name = "nameKey";
2-将
Name
变量设为静态,以便您可以在第二个活动中对其进行访问: public static String Name = "nameKey";
And in your second activity, you can access it this way: 在第二项活动中,您可以通过以下方式访问它:
String value = settings.getString(RelativeLayout.Name, "Default_Value");
You need to declare Name before onCreate() in your second activity too to be able to reference it. 您还需要在第二个活动中的onCreate()之前声明Name才能引用它。 Add:
加:
public String Name = "nameKey";
in your RelativeLayout activity, put this in your onClick
callback : 在您的RelativeLayout活动中,将其放在
onClick
回调中:
public class RelativeLayout extends AppCompatActivity {
...
...
public static String Name = "nameKey";
public static String Email = "emailKey";
public static String Phone = "phoneKey";
...
...
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Name, n);
editor.putString(Email, e);
editor.putString(Phone, p);
editor.apply();
}
MainActivity.java: MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
...
SharedPreferences sharedPreferences = getPreferences(Context.MODE_PRIVATE);
String nameValue = sharedPreferences.getString(RelativeLayout.Name, "Default_Name_Value");
String emailValue = sharedPreferences.getString(RelativeLayout.Email, "Default_Email_Value");
String phoneValue = sharedPreferences.getString(RelativeLayout.Phone, "Default_Phone_Value");
// Do something with the values you retrieve
}
You do not need to hold a reference to your sharedPreferences
inside your activities during the process. 在此过程中,您
sharedPreferences
在活动中保留对您的sharedPreferences
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.