【问题标题】:Shared preferences doesn't store int data共享首选项不存储 int 数据
【发布时间】:2015-10-17 07:57:06
【问题描述】:

我有一个包含两个显示 int 值的 TextView 的活动。当用户单击按钮时,这些值会逐渐变化(1、2、3 等等...)。我使用SharedPreferences 通过按钮单击来存储该值。当我关闭应用程序并再次打开它时,这些值会正确显示在 TextViews 中,但如果它们发生变化,则应该从之前存储的值中添加它们。问题是它们从零开始计数。

这是我的代码:

public class Roulette1 extends ActionBarActivity {

Button button0, button1;
int click_button0, click_button1;

public static final String button0Str = "button0Key";
public static final String button1Str = "button1Key";
public static final String MyPREFERENCES = "MyPrefsRoulette1";
SharedPreferences sharedpreferences;

TextView times_0_tv;
TextView times_1_tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout1);

    times_0_tv = (TextView) findViewById(R.id.times_0);
    times_1_tv = (TextView) findViewById(R.id.times_1);

    button0 = (Button) findViewById(R.id.button0);
    button1 = (Button) findViewById(R.id.button1);

    final TextView total_clicks_tv = (TextView) findViewById(R.id.total_clicks);

    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            click_button1 = click_button1 + 1;
            int total_clicks = click_button0 + click_button1;
            total_clicks_tv.setText(String.valueOf(total_clicks));

            times_0_tv.setText(click_button0);
            times_1_tv.setText(click_button1);

    button0.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            click_button0 = click_button0 + 1;
            int total_clicks = click_button0 + click_button1;
            total_clicks_tv.setText(String.valueOf(total_clicks));

            times_0_tv.setText(click_button0);
            times_1_tv.setText(click_button1);

        }
    });

    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if (sharedpreferences.contains(button0Str))
    {
        times_0_tv.setText(sharedpreferences.getString(button0Str, ""));
    }
    if (sharedpreferences.contains(button1Str))
    {
        times_1_tv.setText(sharedpreferences.getString(button1Str, ""));
    }
}

public void run1(View view) {

    SharedPreferences.Editor editor = sharedpreferences.edit();

    String times0string = times_0_tv.getText().toString();
    String times1string = times_1_tv.getText().toString();
    editor.putString(button0Str, times0string);
    editor.putString(button1Str, times1string);

    editor.commit();
}

希望你有任何想法。谢谢!

【问题讨论】:

  • Problem is that they start to count from zero.。幸运的人。您甚至没有将它们初始化为零。或者另一个值。你应该得到一个编译时警告。
  • Shared preferences doesn't store int data。错误的。您正在尝试存储字符串。不是整数。

标签: android integer sharedpreferences textview android-button


【解决方案1】:

当您从 sharedPrefs 读取数据时,请记住更新字段计数器,而不仅仅是 textViews 的值。

正如 cmets 中所建议的,一个可能的解决方案是使用 textView 值作为状态,直接更新它。否则,您必须手动更新计数器,例如在更新 textView 值的同时更新字段值。就个人而言,我更喜欢将状态与演示文稿分开,以便稍后使用该值更容易计算某些东西(缺点是您必须保持视图同步。这可能会随着the new data binding library 而改变)。

PS我特意没有放任何代码,因为解决方法很琐碎,还有其他的代码答案,但更重要的是因为我认为数据绑定库是处理这类问题的一种更干净的方法,甚至虽然它仍处于测试阶段。

【讨论】:

  • 这是正确的答案,int 计数器变量永远不会使用来自共享首选项的数据进行初始化:click_button0 和 click_button1。不过,您确实应该详细说明您的答案。
  • 最好的办法是取消它们。然后在递增时从 textview 中读取旧值。
【解决方案2】:

Sharedpreferences 也存储 int 数据。检查此链接: http://androidexample.com/Android_SharedPreferences_Basics/index.php?view=article_discription&aid=126&aaid=146

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
    Editor editor = pref.edit();


/**************** Storing data as KEY/VALUE pair *******************/

    editor.putBoolean("key_name1", true);           // Saving boolean - true/false
    editor.putInt("key_name2", "int value");        // Saving integer
    editor.putFloat("key_name3", "float value");    // Saving float
    editor.putLong("key_name4", "long value");      // Saving long
    editor.putString("key_name5", "string value");  // Saving string

    // Save the changes in SharedPreferences
    editor.commit(); // commit changes

我认为您的代码中有一些逻辑错误。请检查。

【讨论】:

    【解决方案3】:

    将此代码添加到 oncreate 的开头:

    SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    
    times0string = String.valueOf(sharedPreferences.getString(button0Str, 0));
    times1string = String.valueOf(sharedPreferences.getString(button1Str, 0));
    

    【讨论】:

      【解决方案4】:

      要解决您的问题,请尝试以下代码:

      替换button1 onClick()中的前两行

      click_button1 = Integer.parseInt(sharedpreferences.getString(button1Str, "")) + 1;
      int total_clicks = Integer.parseInt(sharedpreferences.getString(button0Str, "")) + click_button1;
      

      替换button0 onClick()中的前两行

      click_button0 = Integer.parseInt(sharedpreferences.getString(button0Str, "")) + 1;
      int total_clicks = click_button0 + Integer.parseInt(sharedpreferences.getString(button1Str, ""));
      

      【讨论】:

        猜你喜欢
        • 2013-09-16
        • 2015-08-18
        • 1970-01-01
        • 2019-08-02
        • 1970-01-01
        • 1970-01-01
        • 2018-06-10
        • 1970-01-01
        • 2011-09-03
        相关资源
        最近更新 更多