在另一个活动中检索sharedpreferences字符串

时间:2015-11-15 14:10:53

标签: java android android-sharedpreferences

我正在尝试使用sharedpreferences在一个活动中保存一些edittext字段,然后将这些保存的值输出到另一个活动的textview字段。数据似乎在第一个活动中保存正常,但我无法在新活动中解析存储的变量。有谁知道我做错了什么?谢谢!

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();

第二项活动如下。我试图在共享首选项中检索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");

3 个答案:

答案 0 :(得分:1)

您的变量Name仅在您的第一个活动的范围内定义。您需要在第二个活动中访问它... 你可以做两件事:

1 - 在第二个活动中声明变量Name,其值与第一个活动相同。 2 - 将Name变量设为静态,以便在第二个活动中访问它: public static String Name = "nameKey"; 在第二个活动中,您可以通过以下方式访问它:

String value = settings.getString(RelativeLayout.Name, "Default_Value"); 

答案 1 :(得分:0)

您需要在第二个活动中的onCreate()之前声明名称,以便能够引用它。添加:

public String Name = "nameKey";

答案 2 :(得分:0)

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:

@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
}

在此过程中,您无需在活动中保留对sharedPreferences的引用。