2016-12-02 27 views
0

Iam無法在共享偏好中存儲值。活動關閉後,沒有值會保存。在再次啓動活動時,用於存儲和提取數據的id具有空值。這是我的code.i我沒有附加布局,因爲他們將沒有任何意義。 由於我是新來android.There可能是一些簡單的事情,我一個missing.Please幫助SharedPrefereces無法保存值

public class enter_db extends AppCompatActivity 
    { 
    String field; 
    EditText usrtext,idtxt,bananatxt,coconuttxt,timbertxt,bambootxt,goldtxt,garage1txt,garage2txt,garage3txt,garage4txt,garage5txt; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.db_entry); 
    usrtext=(EditText)findViewById(R.id.username); 
    idtxt=(EditText)findViewById(R.id.UserID); 
    bananatxt=(EditText)findViewById(R.id.banana_count); 
    coconuttxt=(EditText)findViewById(R.id.coconut_count); 
    bambootxt=(EditText)findViewById(R.id.banana_count); 
    timbertxt=(EditText)findViewById(R.id.Timber_count); 
    goldtxt=(EditText)findViewById(R.id.gold_count); 
    garage1txt=(EditText)findViewById(R.id.garage_1); 
    garage2txt=(EditText)findViewById(R.id.garage_2); 
    garage3txt=(EditText)findViewById(R.id.garage_3); 
    garage4txt=(EditText)findViewById(R.id.garage_4); 
    garage5txt=(EditText)findViewById(R.id.garage_5); 
    SharedPreferences pref=getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
    final SharedPreferences.Editor editor = pref.edit(); 
    if(pref.getString("Username",null)!="") 
    { 
    field=pref.getString("Username",null); 
     usrtext.setHint("Username"+field); 
    } 
    if(pref.getString("UserID",null)!=null) 
    { 
     field=pref.getString("UserID",null); 
     idtxt.setHint("UserID: "+field); 
    } 
    if(pref.getString("Banana",null)!=null) 
    { 
     field=pref.getString("Banana",null); 
     bananatxt.setHint("Banana: "+field); 
    } 
    if(pref.getString("Coconut",null)!=null) 
    { 
     field=pref.getString("Coconut",null); 
     coconuttxt.setHint("Cococnut: "+field); 
    } 
    if(pref.getString("Timber",null)!=null) 
    { 
     field=pref.getString("Timber",null); 
     timbertxt.setHint("Timber: "+field); 
    } 
    if(pref.getString("Bamboo",null)!=null) 
    { 
     field=pref.getString("Bamboo",null); 
     bambootxt.setHint("Bamboo"+field); 
    } 
    if(pref.getString("Gold",null)!=null) 
    { 
     field=pref.getString("Gold",null); 
     goldtxt.setHint("Gold: "+field); 
    } 
    if(pref.getString("Garage1",null)!=null) 
    { 
     field=pref.getString("Garage1",null); 
     garage1txt.setHint("Garage1 :"+field); 
    } 
    if(pref.getString("Garage2",null)!=null) 
    { 
     field=pref.getString("Garage2",null); 
     garage2txt.setHint("Garage2 :"+field); 
    } 
    if(pref.getString("Garage3",null)!=null) 
    { 
     field=pref.getString("Garage3",null); 
     garage3txt.setHint("Garage3 :"+field); 
    } 
    if(pref.getString("Garage4",null)!=null) 
    { 
     field=pref.getString("Garage4",null); 
     garage4txt.setHint("Garage4 :"+field); 
    } 
    if(pref.getString("Garage5",null)!=null) 
    { 
     field=pref.getString("Garage5",null); 
     garage5txt.setHint("Garage5 :"+field); 
    } 

    editor.putString("Username",usrtext.getText().toString()); 
    editor.putString("UserID",idtxt.getText().toString()); 
    editor.putString("Banana",bananatxt.getText().toString()); 
    editor.putString("Coconut",coconuttxt.getText().toString()); 
    editor.putString("Timber",timbertxt.getText().toString()); 
    editor.putString("Bamboo",bambootxt.getText().toString()); 
    editor.putString("Gold",goldtxt.getText().toString()); 
    editor.putString("Garage1",garage1txt.getText().toString()); 
    editor.putString("Garage2",garage2txt.getText().toString()); 
    editor.putString("Garage3",garage3txt.getText().toString()); 
    editor.putString("Garage4",garage4txt.getText().toString()); 
    editor.putString("Garage5",garage5txt.getText().toString()); 
    ImageButton ok=(ImageButton)findViewById(R.id.ok); 
    ok.setOnClickListener(new View.OnClickListener() { 
     // Start new list activity 
     public void onClick(View v) 
     { 
      editor.apply(); 
      Intent i=new Intent(getApplicationContext(),Garage.class); 
      startActivity(i); 
     } 
    }); 
} 

}

+0

嘗試更改editor.putString(「Garage4」,garage4txt.getText()。toString())。apply();在每一行 –

+0

使用'.commit()'或'.apply()'保存對共享前綴 – Gattsu

+0

的更改點擊「ok」按鈕時執行 – Yash

回答

0

ü錯過了申請()/提交()來保存chanegs。

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit(); 
editor.putString("name", "Elena"); 
editor.putInt("idName", 12); 
editor.apply(); // editor.commit() 

apply()更快,異步同時commit()同步。

+0

單擊「確定」按鈕時執行此操作 – Yash

0
editor.putString("Garage5",garage5txt.getText().toString()); 
editor.commit(); 
+1

請通過簡短說明更新您的答案,以提高其質量。 – nullpointer

0
在共享偏好

保存價值:從共享偏好

SharedPreferences settings =getSharedPreferences("AppName", 0); 
      SharedPreferences.Editor editor = settings.edit(); 
      editor.putString(key, value); 
    editor. putBoolean(key, value); 
      editor.commit(); 

GET值:

SharedPreferences settings = getSharedPreferences("AppName", 0); 
      String value=settings.getString(key, ""); 
    boolean value=settings.getBoolean(key,false); 
0

正如你不保存編輯文本的價值在按鈕共享偏好上面的代碼點擊,試試這個方法

ok.setOnClickListener(new View.OnClickListener() {
 // Start new list activity
 public void onClick(View v)
 {
 editor.putString("Username",usrtext.getText().toString());
 editor.putString("UserID",idtxt.getText().toString());
 editor.putString("Banana",bananatxt.getText().toString());
 editor.putString("Coconut",coconuttxt.getText().toString());
 editor.putString("Timber",timbertxt.getText().toString());
 editor.putString("Bamboo",bambootxt.getText().toString());
 editor.putString("Gold",goldtxt.getText().toString());
 editor.putString("Garage1",garage1txt.getText().toString());
 editor.putString("Garage2",garage2txt.getText().toString());
 editor.putString("Garage3",garage3txt.getText().toString());
 editor.putString("Garage4",garage4txt.getText().toString());
 editor.putString("Garage5",garage5txt.getText().toString());

 editor.apply();
 Intent i=new Intent(getApplicationContext(),Garage.class);
 startActivity(i);
 }
});