【问题标题】:SharedPrefereces Not able to save ValuesSharedPreferences 无法保存值
【发布时间】:2016-12-02 04:41:43
【问题描述】:

我无法将值存储在共享首选项中。一旦活动关闭,就不会保存任何值。再次启动活动时,用于存储和获取数据的 id 具有空值。 这是我的代码。我没有附加布局,因为它们没有意义。 由于我是 android 新手。我可能缺少一些简单的东西。请帮助

    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() 将更改保存到 sharedprefrence
  • 点击“确定”按钮时这样做
  • 将所有editor.putString() 调用移到onClick() 内部,在editor.apply(); 之前。
  • 成功了。感谢@MikeM 的帮助。

标签: android


【解决方案1】:

你错过了 apply() / commit() 来保存变更。

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

apply() 更快且异步,而 commit() 是同步的。

【讨论】:

  • 点击“确定”按钮时这样做
【解决方案2】:

在共享偏好中保存价值:

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

从共享偏好中获取价值:

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

【讨论】:

    【解决方案3】:

    在上面的代码中,您没有在单击按钮时将编辑文本的值保存在共享首选项中,请尝试这种方式

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

    【讨论】:

      【解决方案4】:
       editor.putString("Garage5",garage5txt.getText().toString());
       editor.commit();
      

      【讨论】:

      • 请更新您的答案并附上简短的解释以提高其质量。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多