简体   繁体   中英

SharedPreferences in Android Studio Clicker App Not Working

I am creating a simple clicker program. I am clicking a pear picture and then it increments by 1 and displays that. I want to save this number so I they can click and then completely exit the app and when they return the same number they left off at is still there. This is my code but yet it still does not save that pears int when I restart the app.

CODE:

    package pearclicker.pearclicker;

    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ImageButton;
    import android.widget.TextView;

    public class MainActivity extends AppCompatActivity {

    ImageButton imageButtonPear;
    TextView showValue;
    int pears;

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

        showValue = (TextView) findViewById(R.id.countText);
    }


    public void PearIncrease(View v) {
        SharedPreferences pref = getApplicationContext().getSharedPreferences("PearCount", MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putInt("pearCount", pears);
        pears++;
        if (pears == 1) {
        showValue.setText(pears + " pear");
            editor.putInt("pearCount", pears);
            editor.apply();
        }
        else {
            showValue.setText(pears + " pears");
            editor.putInt("pearCount", pears);
            editor.apply();
        }
    }

}

Your code is doing fine when clicking the button to increase the value of your pear. but you're doing nothing in your onCreate() so you think that the SharedPreference is not storing the value of pear when you restart the app..

to do that you need some modification to your code.

public class MainActivity extends AppCompatActivity {

    ImageButton imageButtonPear;
    TextView showValue;
    int pears = 0;

    SharedPreferences pref;
    SharedPreferences.Editor editor;

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

        pref = getApplicationContext().getSharedPreferences("PearCount", MODE_PRIVATE);

        showValue = (TextView) findViewById(R.id.countText);

        pears = pref.getInt("pearCount", 0); // This will get the value of your pearCount, It will return Zero if its empty or null.
        showValue.setText(pears + " pears");
    }


    public void PearIncrease(View v) {
        editor = pref.edit();
        pears++;
        showValue.setText(pears + " pears");
        editor.putInt("pearCount", pears);
        editor.apply();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM