i have this code for get value of SharedPreferences from edittext and then send it to other activity somehow i cannot use it from other activity what i miss in my code any suggestions thanks in advance
-thats my first activity that for get SharedPreferences from edittext and i want to send value from it to the second activity
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();
}
});
}
}
and here my second activity that i want receive the value in
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();
}
});
}
}
In your second activity you are trying to get your sharedpreferences instance with different name and also getting your saved value with different key.
Replace this:
SharedPreferences prefs = getSharedPreferences("Name", MODE_PRIVATE);
final String value = prefs.getString("n", "0");
with
SharedPreferences prefs = getSharedPreferences("MyPrefs", MODE_PRIVATE);
final String value = prefs.getString("nameKey", "0");