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

时间:2016-09-11 06:11:47

标签: java android sharedpreferences

我正在尝试使用共享首选项保存数据,并使用TextView在另一个活动中检索该数据。 这里的问题是我能够成功存储数据但无法从另一个活动中检索数据。我还需要在代码中更改以从其他活动中检索数据。

  

EditText ed1,ed2,ed3;
      按钮b1;
      public static final String MyPREFERENCES =" MyPrefs" ;
      public static final String Name =" nameKey&#34 ;;
      public static final String Phone =" phoneKey&#34 ;;
      public static final String Email =" emailKey";
      SharedPreferences共享偏好;

主要活动



 @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



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

        }

2 个答案:

答案 0 :(得分:2)

您在actiongetSharedPreferences()方法中使用了错误的密钥。此外,模式必须相同。

你应该这样做:

getString()

答案 1 :(得分:0)

两件事情确保共享首选项名称“我的首选项”在两种方式中都是通用的,如果您以下列相同的方式存储和检索,则共享:

储存:

null

撷取:

Human

如果您有任何疑问,请在下方发表评论。

相关问题