问题
I want to save user input to SharedPreferences when i click a button so that when a user launches the activity again the user input is set to the EditText and TextView. I already have a sharedPreferences File created that i save some text to in a previous activity. I want update the SharedPreferences File with another details from this new activity that i created.
This is code for creating and saving to SharedPreferences
private SharedPreferences prefs;
public static final String PREF_FILE_NAME = "AuthUser";
prefs = this.getSharedPreferences(PREF_FILE_NAME, Context.MODE_PRIVATE);
txtDate = (TextView) findViewById(R.id.txtDate);
addIdButton = (Button) findViewById(R.id.btnAddId);
addIdButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
expiryDate = txtDate.getText().toString();
edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
idNumber = edtIdNumber.getText().toString();
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("idType", idType);
editor.putString("idNumber", idNumber);
editor.putString("expiryDate", expiryDate);
editor.apply();
}
});
This is my code to set the value from shared preferences to EditText and TextView
@Override
public void onStart(){
super.onStart();
File f = new File(PREF_FILE_NAME);
if (f.exists()){
idType = prefs.getInt("idType", 0);
idNumber = prefs.getString("idNumber", "");
expiryDate = prefs.getString("expiryDate", "");
edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
txtDate = (TextView) findViewById(R.id.txtDate);
txtDate.setText(expiryDate);
edtIdNumber.setText(idNumber);
edtIdNumber.setText(idNumber);
txtDate.setText(expiryDate);
}
}
回答1:
File f = new File(PREF_FILE_NAME);
just has the name of the preference so
f.exists()
will always returns false
Solution : Remove
File f = new File(PREF_FILE_NAME);
if (f.exists()){
because if you don't have any value then simply use the default value
@Override
public void onStart(){
super.onStart();
//File f = new File(PREF_FILE_NAME); // not a right path
//if (f.exists()){ // false
idType = prefs.getInt("idType", 0);
idNumber = prefs.getString("idNumber", "");
expiryDate = prefs.getString("expiryDate", "");
edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
txtDate = (TextView) findViewById(R.id.txtDate);
txtDate.setText(expiryDate);
edtIdNumber.setText(idNumber);
edtIdNumber.setText(idNumber);
txtDate.setText(expiryDate);
//}
}
Alternatively you can use contains
回答2:
To Achive your work: First of all try to understand difference between onCreate() and onStart() method of Activity in android, follow following url: https://developer.android.com/guide/components/activities/activity-lifecycle.html -: use onCreate() method instead of onStart() for setting ui variables;
secondly change in your code like:
public void onClick(View view) {
expiryDate = txtDate.getText().toString();
edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
idNumber = edtIdNumber.getText().toString();
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("idType", idType);
editor.putString("idNumber", idNumber);
editor.putString("expiryDate", expiryDate);
editor.commit();
}
回答3:
just remove if (f.exists())
condition from Onstart()
because File f = new File(PREF_FILE_NAME);
just has the name of the preference so f.exists()
will always returns false
like below code
@Override
public void onStart(){
super.onStart();
/* File f = new File(PREF_FILE_NAME);
if (f.exists()){*/
idType = prefs.getInt("idType", 0);
idNumber = prefs.getString("idNumber", "");
expiryDate = prefs.getString("expiryDate", "");
edtIdNumber = (EditText) findViewById(R.id.edtIdNumber);
txtDate = (TextView) findViewById(R.id.txtDate);
txtDate.setText(expiryDate);
edtIdNumber.setText(idNumber);
txtDate.setText(expiryDate);
}
来源:https://stackoverflow.com/questions/45931720/save-user-input-to-sharedprefernces-and-set-value-to-editext