从共享preferences继续整数计数器 [英] Continuing integer counter from sharedpreferences

查看:92
本文介绍了从共享preferences继续整数计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有对不起,如果我的问题并没有真正意义。首先

基本上我想提出一个理货它使用计数器应用 INT ++ 来增加数量。我设法保存任何数字用户计数值上升到使用共享preferences 。一旦应用程序被关闭,并releaunched数量仍然存在,但一旦用户presses一个按钮来添加或从0再次重新启动计数器减。我怎样才能使整数计数器从离开的地方掉最后一个(如果用户数达20并重新启动应用程序,他们可以继续从20计数,而不是由零开始重新例子是)继续。也许如果你看过code这将使有点更有意义。

 公共类MainActivity扩展活动实现OnClickListener {公共静态最后弦乐preFS =例如preFS按钮B1;
按钮B2;
TextView的TV1;
TextView中TV2;
INT计数器;
INT计数器2;
串stringCounter;
串stringCounter2;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);    B1 =(按钮)findViewById(R.id.button1);
    B2 =(按钮)findViewById(R.id.button2);
    TV1 =(的TextView)findViewById(R.id.textView1);    b1.setOnClickListener(本);
    b2.setOnClickListener(本);    共享preferences例如= getShared preferences(preFS,计数器);
    字符串userString = example.getString(userMessage,未找到);
    tv1.setText(userString);}@覆盖
公共无效的onClick(视图v){
    // TODO自动生成方法存根
    如果(V == B1){        反++;
        stringCounter = Integer.toString(柜);
        tv1.setText(stringCounter);        共享preferences例如preFS = getShared preferences(preFS,计数器);
        编辑编辑=例如prefs.edit();
        editor.putString(userMessage,stringCounter);
        editor.commit();    }    如果(V == B2){
        计数器 - ;
        stringCounter = Integer.toString(柜);
        tv1.setText(stringCounter);        共享preferences例如preFS = getShared preferences(preFS,计数器);
        编辑编辑=例如prefs.edit();
        editor.putString(userMessage,stringCounter);
        editor.commit();
    }
}
}


解决方案

你没有当你重新启动你的应用程序分配计数器的值。因此,它是从零开始,你应该补充这一点。

 共享preferences例如= getShared preferences(preFS,计数器);
字符串userString = example.getString(userMessage,未找到);
tv1.setText(userString);
计数器=的Integer.parseInt(userString); //这一行。

添加的行会给出专柜上届会议的价值。 P.S:您可能需要使用把它包起来试试

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

保存共享preferences更好的办法是做在的onPause(),所以你只要你离开活动做一次。优于每次按钮被点击时调用它。

所以应该像下面这样:

 公共类MainActivity扩展活动实现OnClickListener {公共静态最后弦乐preFS =例如preFS按钮B1;
按钮B2;
TextView的TV1;
TextView中TV2;
INT计数器;
INT计数器2;
串stringCounter;
串stringCounter2;
共享preferences例子;让你一次申报,并使其随处访问//取得共享preferences全球。@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);B1 =(按钮)findViewById(R.id.button1);
B2 =(按钮)findViewById(R.id.button2);
TV1 =(的TextView)findViewById(R.id.textView1);b1.setOnClickListener(本);
b2.setOnClickListener(本);例如= getShared preferences(preFS,计数器);
字符串userString = example.getString(userMessage,未找到);
tv1.setText(userString);}@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
如果(V == B1){    反++;
    stringCounter = Integer.toString(柜);
    tv1.setText(stringCounter);}如果(V == B2){
    计数器 - ;
    stringCounter = Integer.toString(柜);
    tv1.setText(stringCounter);
}
}@覆盖
保护无效的onPause(){//的onPause()将当您离开您的活动被称为临时或永久。
    // TODO自动生成方法存根
    super.onPause();
    编辑编辑= example.edit();
    editor.putString(userMessage,stringCounter);
    editor.commit();
}}

您已经减少在共享preferences每次节省成本这种方式,您单击一次仅

First of all i am sorry if my question doesn't really make sense.

Basically i am making a tally counter app which uses int++ to increase the number. I have managed to save whatever number the user has counted up to using SharedPreferences. Once the app is closed and releaunched the number is still there but once the user presses a button to add or subtract from the counter it restarts from 0 again. How can i make the integer counter continue from where it left off last (and example would be if the user counted up to 20 and relaunched the app they could continue counting from 20 instead of restarting from zero). Maybe if you read the code this will make a little more sense.

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

解决方案

You didnt assign the value of the counter when you restarted your application. so It is started from zero you should add this.

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

the added line will give the counter the value of the last session. P.S: you may need to wrap it with try and catch

-----------Edit------------

The better way to save shared Preferences is to do it in onPause() so you do it once whenever you leave the Activity. better than call it every time the button has been clicked

so it should like the following:

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

}

This way you have reduced the cost of saving in the shared preferences every time you click to once only

这篇关于从共享preferences继续整数计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆