作为标题,我想在编辑textView后获取新字符串,
在我编辑它并再次打开应用程序后,
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);
}
}
答案 0 :(得分:1)
只需创建一个Session.java类
public class Session {
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "SessionVariable";
public static final String KEY_ID = "id";
public Session(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String id) {
editor.putString(KEY_ID, id);
editor.commit();
}
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_ID, pref.getString(KEY_ID, null));
return user;
}
}
将值放入SharedPreference:
Session sm;
String id = ed1.getText().toString();
sm = new Session(MainActivity.this);
sm.createLoginSession(id);
从SharedPreference中检索:
Session sm;
sm = new Session(Welcome.this);
HashMap<String, String> user = sm.getUserDetails();
String id = user.get(Session.KEY_ID);
ed1.setText("Id = " + id);
答案 1 :(得分:0)
根据猜测答案。
创建新活动时,您将数据放入SharedPreference中。
例如,在启动画面中,您首次将数据添加到SharedPereference。当下次输入相同的启动画面并尝试在SharedPreference中添加一些值时,可能在下次提取时您的值为空或错误。
在您的情况下,您在onStop()
中呼叫oncreate()
。所以该值必须为空。
检查以下内容。你必须修改它。我只是想说明为什么你没有得到任何价值。
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();
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));
onStop();
}
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);
}
}
答案 2 :(得分:0)
我认为这一行应该是;
SharedPreferences sharePreferences = getSharedPreferences(PREFERENCES_NAME, 0);
而不是;
SharedPreferences sharePreferences = getSharedPreferences("userName", 0);