Continuing integer counter from sharedpreferences

后端 未结 3 1178
南笙
南笙 2020-12-22 09:21

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 t

相关标签:
3条回答
  • 2020-12-22 09:54

    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

    0 讨论(0)
  • 2020-12-22 10:01

    You need to see the description of getSharedPreferences. The second argument is Mode. I think you'd better use 0.

    0 讨论(0)
  • 2020-12-22 10:15

    Just putting my comment in answer:

     @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
    
        }
    
    0 讨论(0)
提交回复
热议问题