从另一个活动接收共享SharedPreferences值的值

时间:2017-02-20 17:52:50

标签: android sharedpreferences android-sharedpreferences

我有这个代码从edittext获取SharedPreferences的值,然后将它发送到其他活动我不能从其他活动使用它我在我的代码中想念任何建议 提前谢谢

- 我的第一个活动是从edittext获取SharedPreferences,我想从它发送值到第二个活动

public class NationalId extends Activity {
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
SharedPreferences sharedpreferences;

final Context context = this;
private Button button;
private TextView result;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_national_id);

    // components from main.xml
    button = (Button) findViewById(R.id.button1);
    result = (TextView) findViewById(R.id.tv1);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

    // add button listener
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {


            // get prompts.xml view
            LayoutInflater li = LayoutInflater.from(context);
            View promptsView = li.inflate(R.layout.prompts, null);

            android.app.AlertDialog.Builder alertDialogBuilder = new android.app.AlertDialog.Builder(
                    context);

            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);

            final EditText userInput = (EditText) promptsView
                    .findViewById(R.id.editTextDialogUserInput);

            alertDialogBuilder
                    .setCancelable(false)
                    .setPositiveButton("OK",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    String n  = userInput.getText().toString();
                                    SharedPreferences.Editor editor = sharedpreferences.edit();
                                    editor.putString(Name, n);
                                    editor.commit();
                                    // edit text
                                    result.setText(userInput.getText());
                                    Toast.makeText(NationalId.this,"saved:"+n,Toast.LENGTH_LONG).show();

                                }
                            })
                    .setNegativeButton("Cancel",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,int id) {
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            android.app.AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

        }
    });
}
}

这是我想要的第二个活动

public class receive extends Activity {



private Button button;
private EditText etPhoneno;

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_opreator_mobily);
    button = (Button) findViewById(R.id.buttonCall);
    etPhoneno = (EditText) findViewById(R.id.editText1);
    SharedPreferences prefs = getSharedPreferences("Name",
            MODE_PRIVATE);
    final String value = prefs.getString("n", "0");
    // add button listener\
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            Toast.makeText(receive.this,"saved:"+value,Toast.LENGTH_LONG).show();


        }
    });
}

}

2 个答案:

答案 0 :(得分:5)

在您的第二个活动中,您尝试使用不同的名称获取您的sharedpreferences实例,并使用不同的密钥获取您保存的值。

替换它:

SharedPreferences prefs = getSharedPreferences("Name", MODE_PRIVATE);
final String value = prefs.getString("n", "0");

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");

答案 1 :(得分:0)

替换以下两行

SharedPreferences prefs = getSharedPreferences("Name",MODE_PRIVATE);
    final String value = prefs.getString("n", "0"); 

这些行

SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");