Android 从SharedReferences继续整数计数器

Android 从SharedReferences继续整数计数器,android,Android,基本上,我正在制作一个计数计数器应用程序,它使用int++来增加数字。我已经设法保存了用户使用SharedReferences统计的任何数字。一旦应用程序关闭并重新启动,数字仍然存在,但一旦用户按下按钮从计数器中添加或减去,它将从0重新启动。如何使整数计数器从上次停止的位置继续(例如,如果用户数到20,然后重新启动应用程序,他们可以从20开始继续计数,而不是从零开始重新启动)。下面是提供一些上下文的代码: public class MainActivity extends Activity im

基本上,我正在制作一个
计数
计数器应用程序,它使用
int++
来增加数字。我已经设法保存了用户使用
SharedReferences
统计的任何数字。一旦应用程序关闭并重新启动,数字仍然存在,但一旦用户按下按钮从计数器中添加或减去,它将从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();
    }
}
}

重新启动应用程序时,您没有分配计数器的值。所以它从零开始,你应该加上这个

SharedPreferences example = getSharedPreferences(PREFS, counter);
String userString = example.getString("userMessage", "Nothing Found");
tv1.setText(userString);
counter = Integer.ParseInt(userString); //this line.
添加的行将为计数器提供上次会话的值。提示:您可能需要使用
try
catch

------编辑------------

保存共享首选项的更好方法是在
onPause()
中执行此操作,因此无论何时离开
活动
,都要执行一次。总比每次点击按钮都打电话好

因此,它应该如下所示:

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

}

通过这种方式,您可以减少每次单击一次共享首选项时节省的成本

只需回答我的评论即可:

 @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

    }

您需要查看GetSharedReferences的描述。第二个参数是模式。我认为你最好使用0。

你在你的类中将计数器作为变量,你正在增加它,这是错误的。在oncreate中读取的值只需将其存储在conter变量中:counter=integer.parseInt(userString);谢谢,还有没有一种方法可以更有效地将号码保存到SharedReferences,而不是每次更改号码?(可能是在应用程序关闭或类似的情况下)谢谢,还有没有一种方法可以更有效地将号码保存到SharedReferences,而不是每次更改号码?(可能是在应用程序关闭或类似的情况下)请检查编辑的文本,如果有帮助,请将其勾选为右:)并且您可以在SharedReferences中存储整数。无需将其更改为字符串。