来自共享偏好的连续整数计数器

时间:2013-11-19 10:26:58

标签: android

基本上我正在制作tally计数器应用,它使用int++来增加数量。我已设法保存用户使用SharedPreferences计算的任何数字。一旦应用程序关闭并重新启动,数字仍然存在,但是一旦用户按下按钮添加或减少计数器,它将再次从0重新启动。如何使整数计数器从最后一次停止的位置继续(例如,如果用户计数到20并重新启动应用程序,他们可以从20继续计数而不是从零重新启动)。以下是提供上下文的代码:

public class MainActivity extends Activity implements OnClickListener{

public static final String PREFS = "examplePrefs";

Button b1;
Button b2;
TextView tv1;
TextView tv2;
int counter;
int counter2;
String stringCounter;
String stringCounter2;

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

    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    tv1 = (TextView) findViewById(R.id.textView1);

    b1.setOnClickListener(this);
    b2.setOnClickListener(this);

    SharedPreferences example = getSharedPreferences(PREFS, counter);
    String userString = example.getString("userMessage", "Nothing Found");
    tv1.setText(userString);

}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if (v==b1){

        counter++;
        stringCounter = Integer.toString(counter);
        tv1.setText(stringCounter);

        SharedPreferences examplePrefs = getSharedPreferences(PREFS, counter);
        Editor editor = examplePrefs.edit();
        editor.putString("userMessage", stringCounter);
        editor.commit();

    }

    if (v==b2){
        counter--;
        stringCounter = Integer.toString(counter);
        tv1.setText(stringCounter);

        SharedPreferences examplePrefs = getSharedPreferences(PREFS, counter);
        Editor editor = examplePrefs.edit();
        editor.putString("userMessage", stringCounter);
        editor.commit();
    }
}
}

3 个答案:

答案 0 :(得分:0)

重新启动应用程序时,您没有指定计数器的值。所以从零开始你应该添加它。

SharedPreferences example = getSharedPreferences(PREFS, counter);
String userString = example.getString("userMessage", "Nothing Found");
tv1.setText(userString);
counter = Integer.ParseInt(userString); //this line.

添加的行将为计数器提供上一个会话的值。 P.S:你可能需要用trycatch

来包装它

<强> ----------- ------------编辑

保存共享偏好设置的更好方法是在onPause()中执行此操作,以便在您离开Activity时执行此操作。比每次点击按钮时调用它更好

所以它应该如下:

public class MainActivity extends Activity implements OnClickListener{

public static final String PREFS = "examplePrefs";

Button b1;
Button b2;
TextView tv1;
TextView tv2;
int counter;
int counter2;
String stringCounter;
String stringCounter2;
SharedPreferences example; // made shared Preferences global so you declare it once and make it accessible everywhere.

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

b1 = (Button) findViewById(R.id.button1);
b2 = (Button) findViewById(R.id.button2);
tv1 = (TextView) findViewById(R.id.textView1);

b1.setOnClickListener(this);
b2.setOnClickListener(this);

example = getSharedPreferences(PREFS, counter);
String userString = example.getString("userMessage", "Nothing Found");
tv1.setText(userString);

}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v==b1){

    counter++;
    stringCounter = Integer.toString(counter);
    tv1.setText(stringCounter);

}

if (v==b2){
    counter--;
    stringCounter = Integer.toString(counter);
    tv1.setText(stringCounter);
}
}

@Override
protected void onPause() { // onPause() will be called whenever you leave your    activity, temporary or permanently.
    // TODO Auto-generated method stub
    super.onPause();
    Editor editor = example.edit();
    editor.putString("userMessage", stringCounter);
    editor.commit();
}

}

这样,每次单击一次

,您就可以降低共享首选项中保存的成本

答案 1 :(得分:0)

只需将我的评论放在答案中:

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

        b1 = (Button) findViewById(R.id.button1);
        b2 = (Button) findViewById(R.id.button2);
        tv1 = (TextView) findViewById(R.id.textView1);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);

        SharedPreferences example = getSharedPreferences(PREFS, counter);
        String userString = example.getString("userMessage", "Nothing Found");
        tv1.setText(userString);

    counter = Integer.ParseInt(userString); //add this

    }

答案 2 :(得分:0)

您需要查看getSharedPreferences的说明。第二个参数是Mode。我认为你最好使用0。