0

We've been doing this beer app this semester where you can add drinks as standard then press buttons and it will count beers and add a sum for you.

A problem we're having is that when we have pressed the buttons and the count is being summed up. When we close the application on the emulator, it doesn't remember the input. Is there any way to fix this?

Basically, we want it to remember Msum and Mcount, even after the app is closed an re-opened

the code is:

One file

public class ResourceManager {
    private static ResourceManager singleton = new ResourceManager();
    private ResourceManager() {}

    public static ResourceManager getInstance() {
        return singleton;
    }

    protected static int cost_beer = 0;
    protected static int cost_drink = 0;
    protected static int count = 0;
    protected static int sum = 0;
}

Main File:

countButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        ResourceManager.getInstance().count++;
        ResourceManager.getInstance().sum += ResourceManager.getInstance().cost_beer;
        countTextView.setText("Du har drukket " + ResourceManager.getInstance().count + " enheter!");
        sumTextView.setText("Sum:" + ResourceManager.getInstance().sum + "!");
    }
});
drinkButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        ResourceManager.getInstance().count++;
        ResourceManager.getInstance().sum += ResourceManager.getInstance().cost_drink;
        countTextView.setText("You have been drinking " + ResourceManager.getInstance().count + " units!");
        sumTextView.setText("Sum:" + ResourceManager.getInstance().sum + "!");
    }
});
resetButton.setOnClickListener(new View.OnClickListener() {
    public void onClick (View view) {
        ResourceManager.getInstance().sum = 0;
        ResourceManager.getInstance().count = 0;
        countTextView.setText("You have been drinking " + ResourceManager.getInstance().count + " units!");
        sumTextView.setText("Sum:" + ResourceManager.getInstance().sum + "!");
    }
});
Tunaki
  • 125,519
  • 44
  • 317
  • 399

1 Answers1

3

Look at SharedPreferences. It might help you. It stores data in key-value pairs.

See this StackOverflow link for an example.

To save data:

// MY_PREFS_NAME - a static String variable like:  
//public static final String MY_PREFS_NAME = "MyPrefsFile"; 
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,  MODE_PRIVATE).edit();
editor.putString("name", "Elena");
editor.putInt("idName", 12);
editor.commit();

To retrieve:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
  String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
  int idName = prefs.getInt("idName", 0); //0 is the default value.
} 

Here is the official documentation: http://developer.android.com/reference/android/content/SharedPreferences.html

Also another example to help you

Community
  • 1
  • 1
Nathan
  • 1,161
  • 1
  • 15
  • 31