user1985295
user1985295

Reputation: 13

sharedPreferences help. How do I save an int?

I can't for the life of me seem to figure out how to save an int. All I want to do is save leftCounter into a file and retrieve it whenever I reboot my code.

public class SalesTrack extends Activity {

   byte leftCounter = 0;
   byte centerCounter = 0;
   byte rightCounter = 0;
   int total;
   int x, y, z;

   public static final String PREFS = "data";

   TextView counterAAL, counterUPG, counterNL, counterTotal;

   private final String LEFTY = "6";

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

       counterAAL = (TextView) findViewById(R.id.left_number);
       counterUPG = (TextView) findViewById(R.id.center_number);
       counterNL = (TextView) findViewById(R.id.right_number);
       counterTotal = (TextView) findViewById(R.id.total);

       SharedPreferences sharedPref = this.getSharedPreferences(LEFTY, 0);
       sharedPref.getInt("data", leftCounter);
       counterAAL.setText(PREFS);
   }

   public void LeftInc(View v) {
       leftCounter++;
       counterAAL.setText("" + leftCounter);
       Refresh();
   }

   public void LeftDec(View v) {
       leftCounter--;
       counterAAL.setText("" + leftCounter);
       Refresh();
   }

   public void CenterInc(View v) {
       centerCounter++;
       counterUPG.setText("" + centerCounter);
       Refresh();
   }

   public void CenterDec(View v) {
       centerCounter--;
       counterUPG.setText("" + centerCounter);
       Refresh();
   }

   public void RightInc(View v) {
       rightCounter++;
       counterNL.setText("" + rightCounter);
       Refresh();
   }

   public void RightDec(View v) {
       rightCounter--;
       counterNL.setText("" + rightCounter);
       Refresh();
   }

   // Refresh Total after every click.
   public void Refresh() {
       x = leftCounter * 10;
       y = centerCounter * 20;
       z = rightCounter * 35;
       total = x + y + z;
       counterTotal.setText("" + total);

       SharedPreferences sharedPref = getSharedPreferences(PREFS, 0);
       SharedPreferences.Editor editor = sharedPref.edit();
       editor.putInt("data", leftCounter);
       editor.commit();
   }
}

Upvotes: 0

Views: 208

Answers (6)

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

Change like this

SharedPreferences sharedPref = = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("data", leftCounter);
editor.commit();

Upvotes: 0

Vikalp Patel
Vikalp Patel

Reputation: 10887

You are saving your data inside preference named PREFS and fetching through preference nameLEFTY

Try to change your preference name from LEFTY to PREFS under onCreate().

protected void onCreate(Bundle savedInstanceState) 
{
...
SharedPreferences sharedPref = this.getSharedPreferences(PREFS, 0);
}

Upvotes: 0

Samadhan Medge
Samadhan Medge

Reputation: 2049

SharedPreferences sharedPref = getSharedPreferences(LEFTY, 0);
SharedPreferences.Editor editor = sharedPref .edit();               
editor.putInt("data", leftCounter);
editor.commit();

Upvotes: 0

iagreen
iagreen

Reputation: 32016

You get the preferences with two different names LEFTY and PREFS. Given the names, I think you want to change the following line in your onCreate

SharedPreferences sharedPref = this.getSharedPreferences(LEFTY, 0);

to

SharedPreferences sharedPref = this.getSharedPreferences(PREFS, 0);

Upvotes: 0

Guna
Guna

Reputation: 121

You have a mistake in your code.

// SharedPreferences sharedPref = getSharedPreferences(PREFS, 0); (This line is wrong)
      SharedPreferences sharedPref = getSharedPreferences(LEFTY, 0);
      SharedPreferences.Editor editor = sharedPref.edit();
      editor.putInt("data", leftCounter);
      editor.commit();

Upvotes: 2

goravine
goravine

Reputation: 266

so you get the data from the file but it's detected as String ?

you can use the parse function to convert the String into Integer

int counter = Integer.parseInt(stringvalue);

Upvotes: 0

Related Questions