我在SharedPreferences中保存了一些數據,但是當我按下後退按鈕並再次轉到配置文件時,數據將清除。
如何在SharedPreferences的幫助下保存數據?從共享prefrences中檢索數據
package www.edukeen.in.eduaspire;
public class Profile extends AppCompatActivity {
EditText editTextName, editDOB, editphone, editcity, editclass, editboard, editschool, edithobbies, editachievements;
Button buttonSave;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String Name = "nameKey";
public static final String DOB = "DOBKey";
public static final String Phone = "phoneKey";
public static final String City = "cityKey";
public static final String Class = "classKey";
public static final String Board = "boardKey";
public static final String School = "schoolKey";
public static final String Hobbies = "hobbiesKey";
public static final String Achievements = "achievementsKey";
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
editTextName = (EditText) findViewById(R.id.editTextName);
editDOB = (EditText) findViewById(R.id.editDOB);
editphone = (EditText) findViewById(R.id.editphone);
editcity = (EditText) findViewById(R.id.editcity);
editclass = (EditText) findViewById(R.id.editclass);
editboard = (EditText) findViewById(R.id.editboard);
editschool = (EditText) findViewById(R.id.editschool);
edithobbies = (EditText) findViewById(R.id.edithobbies);
editachievements = (EditText) findViewById(R.id.editachievements);
buttonSave=(Button)findViewById(R.id.buttonSave);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
buttonSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String Name = editTextName.getText().toString();
String DOB = editDOB.getText().toString();
String Phone = editphone.getText().toString();
String City = editcity.getText().toString();
String Class = editclass.getText().toString();
String Board = editboard.getText().toString();
String School = editschool.getText().toString();
String Hobbies = edithobbies.getText().toString();
String Achievements = editachievements.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(Name, Name);
editor.putString(DOB, DOB);
editor.putString(Phone, Phone);
editor.putString(City, City);
editor.putString(Class, Class);
editor.putString(Board, Board);
editor.putString(School, School);
editor.putString(Hobbies, Hobbies);
editor.putString(Achievements, Achievements);
editor.commit();
Toast.makeText(Profile.this,"Data saved",Toast.LENGTH_LONG).show();
String name = sharedpreferences.getString("name", "No name");
String dob = sharedpreferences.getString("DOB", "");
String phone = sharedpreferences.getString("phone", "");
String city = sharedpreferences.getString("city", "");
String clas = sharedpreferences.getString("class", "");
String board = sharedpreferences.getString("board", "");
String school = sharedpreferences.getString("school", "");
String hobbies = sharedpreferences.getString("hobbies", "");
String achievements = sharedpreferences.getString("achievements", "");
}
});
}
}
另外我應該怎麼做,如果我想在我的數據庫中獲取這些數據?
我已經使用firebase登錄了一個用戶。
use editor.apply(); – DKV
錯誤是您在保存數據時使用的鍵和值的數據相同,並且您也使用保留關鍵字。 – Meenal
請參閱http://stackoverflow.com/a/29840449/2006803。並添加「這個」。在putString方法的關鍵方面 –