共享首选项不存储int数据

时间:2015-07-27 08:05:18

标签: android integer sharedpreferences textview android-button

我有一个带有两个显示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();
}

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

4 个答案:

答案 0 :(得分:2)

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

正如评论中所建议的,可能的解决方案是使用textView值作为状态,直接更新。否则,您必须手动更新计数器,例如通过在更新textView值的同时更新字段值。就个人而言,我更喜欢将状态与表示分开,以便以后更容易计算具有该值的内容(缺点是您必须保持视图同步。这可能随the new data binding library而变化。)< / p> PS我故意没有放任何代码,因为解决方案很简单,代码还有其他答案,但更重要的是因为我认为数据绑定库是一种更清晰的方式来处理这类问题,甚至虽然它还处于测试阶段。

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

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

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

times0string = String.valueOf(sharedPreferences.getString(button0Str, 0));
times1string = String.valueOf(sharedPreferences.getString(button1Str, 0));

答案 3 :(得分:0)

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

替换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, ""));