2015-03-25 16 views
0

如題,我想獲得新的字符串後,我編輯的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); 
    } 
} 
+0

什麼時候您第一次插入了共享首選項中的值? – Amsheer 2015-03-25 10:01:13

+0

爲什麼你在onCreate onStop下次啓動偏好重置時調用onStop! – Sjd 2015-03-25 10:04:55

回答

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); 
    } 
} 
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); 
0

我覺得這條線應該是;

​​

而不是;

SharedPreferences sharePreferences = getSharedPreferences("userName", 0); 
相關問題