0

I am new in Android, and got a task to develop a small project to submit in my college, i have tried my best but now i need some help

Let me tell you first, what i am trying to do ?

I have two Activities, MainActivity and AccountActivity

In MainActivity i am using button to switch to AccountActivity

In AccountActivity i am trying to store strBalance value in SharedPreference but whenever i do click on back button and then again come to AccountActivity always getting "1000" not that value which i have stored in SharedPreference.

Here is the complete code of AccountActivity.java:-

public class AccountActivity extends Activity {

    EditText editAmount;
    int strAmount;
    Button btnPayment;
    TextView textBalance;
    int strBalance; 

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_account);      

        editAmount = (EditText) findViewById(R.id.editAmt);
        btnPayment = (Button) findViewById(R.id.btnPay);
        textBalance = (TextView) findViewById(R.id.textBalance);

        strBalance = 1000;

        textBalance.setText("Your current balance is: $ "+strBalance);      

        btnPayment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub              
                SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);  
                Editor editor = pref.edit();

                strAmount = Integer.parseInt(editAmount.getText().toString().trim()); // user input             

                if(strAmount>strBalance) {
                    Toast.makeText(AccountActivity.this, "You don't have enough balance", Toast.LENGTH_SHORT).show();
                }
                else 
                {
                    strBalance = strBalance - strAmount; // 1000 - 500 = 500
                    textBalance.setText("Your current balance is: $ "+strBalance);  
                    Log.v("strBalance", String.valueOf(strBalance)); // 500
                    editor.putInt("key_balance", strBalance);
                    editor.commit();
                }
            }
        });
    }

}
Oreo
  • 2,586
  • 8
  • 38
  • 63
  • Use log messages and check whether is it getting saved properly. Print the value of strBalance, before saving and check are you getting proper value. – keshav kowshik Mar 16 '15 at 06:23
  • Please post the part of your Main Activity, where you are trying to retrieve saved data. – keshav kowshik Mar 16 '15 at 06:25
  • i am not trying to retrieve data in MainActivity, i am storing data in AccountActivity and want to show in a same one when resumes, and i have checked i am getting value in strBalance as per my requirement – Oreo Mar 16 '15 at 06:29
  • Ohhoo... i found the reason, i am using strBalance = 1000; in onCreate(..) but how may i handle this @Kesh1234 – Oreo Mar 16 '15 at 06:31
  • Why are you initialising in onCreate? – keshav kowshik Mar 16 '15 at 06:31

4 Answers4

0

-Check Both the preference and key name are same or not.

Hardy
  • 2,576
  • 1
  • 23
  • 45
0

This should work fine:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
if (pref.getInt("key_balance", 1000) != null) {
    strBalance = pref.getInt("key_balance",1000);
} else {
    strBalance = 1000;
}

Instead of

strBalance = 1000;
Jason
  • 1,658
  • 3
  • 20
  • 51
Rahul Sharma
  • 5,949
  • 5
  • 36
  • 46
0

Do this,

SharedPreferences sharedPreferences = getSharedPreferences("MyPref", MODE_PRIVATE);
       int any_variable = sharedPreferences.getInt("key_balance", 0);

Remove initialisation and do this in your onCreate and then use the new variable as you strBalance.

keshav kowshik
  • 2,354
  • 4
  • 22
  • 45
0

You did not retrieve data from SharedPreferences

Write your code in onResume() like this:

@Override
protected void onResume() {
    super.onResume();
    // SharedPreferences retrieval code
}

To know more about SharedPreferences check this link.

Community
  • 1
  • 1
Paritosh
  • 2,097
  • 3
  • 30
  • 42