Reputation: 379
I know this has been asked a lot around SO, but I just can't seem to get this working. Situation : I have a dialog with a EditText in it with an accept button. I would like to store the value of the string with SharedPreferences when the user touch the accept button. Here's the code I have so far.
public void showDialog()
{
final Dialog dialog = new Dialog(VentilationActivity.this);
dialog.setContentView(R.layout.menu_options);
dialog.setTitle("Configuration de l'adresse IP");
dialog.setCancelable(true);
dialog.show();
EditText adressIp = (EditText) dialog.findViewById(R.id.editText1);
SharedPreferences preferences = getSharedPreferences("Agrinuvo", 0);
String texte = preferences.getString("VentIpKey", "");
adressIp.setText(texte);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(adressIp, InputMethodManager.SHOW_IMPLICIT);
Button btnAccept = (Button) dialog.findViewById(R.id.button1);
btnAccept.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
EditText adressIp = (EditText) dialog.findViewById(R.id.editText1);
textIp = adressIp.getText().toString();
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("VentIpKey", textIp);
editor.commit();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(adressIp.getWindowToken(), 0);
dialog.dismiss();
}
});
}
And, of course this is not working. Everytime I close the dialog window and reopen it the EditText text is empty. Thanks for any help you can provide.
Upvotes: 0
Views: 272
Reputation: 4207
I've used preferences in my application without too many problems for quite a while and I would suggest a couple things:
getSharedPreferences() should only be used for preferences shared between different Activities. If only your one Activity will be using the data, use this to save:
SharedPreferences settings = getPreferences (MODE_PRIVATE);
SharedPreferences.Editor ed = settings.edit();
ed.putBoolean ("BooleanKey", booleanVar);
ed.putInt ("IntKey", intVar);
ed.putFloat ("FloatKey", floatVar);
ed.putLong ("LongKey", longVar);
ed.commit();
And this to retrieve:
SharedPreferences settings = getPreferences (MODE_PRIVATE);
longVar = settings.getLong ("longKey", 0);
...
If you're going to share your preferences across Activites, that's when you'll want to use getSharedPreferences(), but you'll want to NOT user MODE_PRIVATE. Currently, I use MODE_WORLD_WRITEABLE in the code where I write the data and MODE_WORLD_READABLE where I read it, that's probably not the best way (at least, the if the warnings I'm getting from Eclipse are to be believed).
Good luck,
R.
Upvotes: 1
Reputation: 507
It looks like there error might be inside your onclick method in the following line
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
If you change it to
SharedPreferences preferences = getSharedPreferences("Agrinuvo", Context.MODE_PRIVATE);
It should work. The way you were calling it, you weren't getting the same preference that you requesting for your dialog.
Upvotes: 1
Reputation: 6073
How about;
public void showDialog() {
....
final SharedPreferences preferences = getSharedPreferences("Agrinuvo", 0);
....
@Override
public void onClick(View v) {
....
// Use previous preferences instance instead.
// SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("VentIpKey", textIp);
editor.commit();
....
}
}
}
Anyway, it seems so that you're writing to different preferences than where you read the default value from.
Upvotes: 1
Reputation: 11807
Inside your onClickObserver
you create a SharedPreference object for a file named after your activity's class name. At least this is what the Activity's getPreferences(int)
documentation states. Try instead initializing that object the same way you do it in showDialog
or making the showDialog
's preferences
final.
Upvotes: 1