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);
}
});
}
}
嘗試更改editor.putString(「Garage4」,garage4txt.getText()。toString())。apply();在每一行 –
使用'.commit()'或'.apply()'保存對共享前綴 – Gattsu
的更改點擊「ok」按鈕時執行 – Yash