简体   繁体   English

共享首选项在活动 A 中存储数据并从活动 B 中检索数据

[英]shared preferences Storing data in Activity A and retrieve data from Activity B

I am trying to save data using Shared Preferences and retrieving that data in another activity using TextView.我正在尝试使用共享首选项保存数据并使用 TextView 在另一个活动中检索该数据。 Issue here is i am able to store data successfully but not able to retrieve data from another activity.这里的问题是我能够成功存储数据但无法从另一个活动中检索数据。 What else do i need to change in my code to retrieve data from another activity..我还需要在代码中更改什么才能从另一个活动中检索数据..

EditText ed1,ed2,ed3; EditText ed1,ed2,ed3;
Button b1;按钮 b1;
public static final String MyPREFERENCES = "MyPrefs" ; public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey"; public static final String Name = "nameKey";
public static final String Phone = "phoneKey"; public static final String Phone = "phoneKey";
public static final String Email = "emailKey"; public static final String Email = "emailKey";
SharedPreferences sharedpreferences; SharedPreferences 共享首选项;

Main Activity主要活动



 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ed1=(EditText)findViewById(R.id.editText);
        ed2=(EditText)findViewById(R.id.editText2);
        ed3=(EditText)findViewById(R.id.editText3);

        b1=(Button)findViewById(R.id.button);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n  = ed1.getText().toString();
                String ph  = ed2.getText().toString();
                String e  = ed3.getText().toString();

                SharedPreferences.Editor editor = sharedpreferences.edit();

                editor.putString(Name, n);
                editor.putString(Phone, ph);
                editor.putString(Email, e);
                editor.commit();
                Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    public void Second_layout(View view)
    {
        Intent i = new Intent(MainActivity.this, retrive.class);
        startActivity(i);

    }

retrieve.java检索.java



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_retrive);
        sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

        TextView tv =(TextView)findViewById(R.id.textView11);
        sharedpreferences  = getSharedPreferences("MyPREFERENCES",0);
        String userString = sharedpreferences.getString("Name","Nothing Found");
        tv.setText(userString);

        }

You are using wrong keys in getSharedPreferences() and getString() methods.Also, the mode must be same.您在getSharedPreferences()getString()方法中使用了错误的键。 getSharedPreferences() ,模式必须相同。

You're supposed to do this :你应该这样做:

sharedpreferences  = getSharedPreferences(MainActivity.MyPREFERENCES, Context.MODE_PRIVATE);
String userString = sharedpreferences.getString(MainActivity.Name,"Nothing Found");
tv.setText(userString);

Two Things make sure that the shared preferences name "My Preferences" is common in both ways and cross if you are storing and retrieved in same manner as followed:两件事确保共享首选项名称“我的首选项”在两种方式中都是通用的,如果您以相同的方式存储和检索,则交叉:

Storing:储存:

 public static void setUserInfo(Context context, Map<String,Object> userInfo){
        SharedPreferences.Editor editor = context.getSharedPreferences(USER_PREFERENCES, context.MODE_PRIVATE).edit();
        editor.putString(USER_INFO,"My Data");
        editor.apply();
    }

Fetching:获取:

public static Map<String,Object> getUserInfo(Context context){
        SharedPreferences prefs = context.getSharedPreferences(USER_PREFERENCES, context.MODE_PRIVATE);
        String userInfo = prefs.getString(USER_INFO,"");
        return userInfo;
    }

Comment below if you have any doubt.如果您有任何疑问,请在下方评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM