如題,我想獲得新的字符串後,我編輯的TextView,我用sharePrereference保存在TextView的字符串,afyer關閉應用程序,並重新打開,它不工作
後,我編輯和轉再次應用,
TextView的仍然是空的,
能有人幫我我錯過了什麼在我的代碼,
預先感謝您的,可以在這裏提供的任何援助。
這裏是我下面的代碼:
public class MainActivity extends ActionBarActivity {
//private SharedPreferences saveUserName;
private AutoCompleteTextView editText1, editText2, editText3,
editText4, editText5, editText6;
private ImageButton imageButton1, imageButton2, imageButton3,
imageButton4, imageButton5, imageButton6;
private final String PREFERENCES_NAME = "userinfo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeViews();
onStop();
getUserName();
SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
editText1.setText(preferences.getString("EditText1", null));
editText2.setText(preferences.getString("EditText2", null));
editText3.setText(preferences.getString("EditText3", null));
editText4.setText(preferences.getString("EditText4", null));
editText5.setText(preferences.getString("EditText5", null));
editText6.setText(preferences.getString("EditText6", null));
}
private void initializeViews(){
editText1 = (AutoCompleteTextView) findViewById(R.id.UserName);
editText2 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView2);
editText3 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView3);
editText4 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView4);
editText5 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView5);
editText6 = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView6);
}
public void onStop() {
super.onStop();
SharedPreferences sharePreferences = getSharedPreferences("userName", 0);
SharedPreferences.Editor editor = sharePreferences.edit();
editor.putString("EditText1", editText1.getText().toString());
editor.putString("EditText2", editText2.getText().toString());
editor.putString("EditText3", editText3.getText().toString());
editor.putString("EditText4", editText4.getText().toString());
editor.putString("EditText5", editText5.getText().toString());
editor.putString("EditText6", editText6.getText().toString());
editor.commit();
}
public void getUserName()
{
SharedPreferences preferences = getSharedPreferences(PREFERENCES_NAME, Activity.MODE_PRIVATE);
editText1.setText(preferences.getString("EditText1", null));
editText2.setText(preferences.getString("EditText2", null));
editText3.setText(preferences.getString("EditText3", null));
editText4.setText(preferences.getString("EditText4", null));
editText5.setText(preferences.getString("EditText5", null));
editText6.setText(preferences.getString("EditText6", null));
}
public void goToUserInfoActivity(View v)
{
Intent it = new Intent(this, UserInfoActivity.class);
startActivity(it);
}
}
什麼時候您第一次插入了共享首選項中的值? – Amsheer 2015-03-25 10:01:13
爲什麼你在onCreate onStop下次啓動偏好重置時調用onStop! – Sjd 2015-03-25 10:04:55