简体   繁体   English

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

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

相关问题 我在从一个活动的共享首选项到另一活动的数据检索中遇到问题 - I have problems in retrieving data from sharedpreferences of one activity to another activity 从 SharedPreferences 检索字体大小到其他活动 - Retrieving font size from SharedPreferences to other activity 如何使用ArrayList <String> 用于保存/检索共享首选项 - How to use ArrayList<String> for saving/retrieving in sharedpreferences 传递多个字符串并在另一活动中检索它们 - Passing more than one string and retrieving them on another activity Android:SharedPreferences不会在其他活动中被拉 - Android: SharedPreferences not getting pulled in another activity 在另一个活动中获取 sharedPreferences 的更新 - get the sharedPreferences's update in another activity sharedpreferences getString() 返回活动三中的空字符串 - sharedpreferences getString () returns the empty string in activity three 在微调器中进行用户选择,将其存储在sharedPreferences中,并在其他活动中使用它 - Taking user selection in a spinner, storing it in sharedPreferences, and using it in another activity 如何使用 sharedpreferences 保存计时器时间并在另一个活动中检索它 - How to save chronometer time and retrieve it in another activity using sharedpreferences 保存并检索ArrayList到SharedPreferences - Save and Retrieving ArrayList to SharedPreferences
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM