• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Textview value does not update from current saved value

Mdzh

Lurker
Hello, I'm a newbie and I need help figuring how to update a textview value. I have it set on sharedpreferences so it loads the value when reloading the app. What I wanted to do is to
add an operation which would add the current value selected into the saved TextView value.

Steps:
1)user select value to deposit
2)the balance is updated on the textview

The error I am getting is that the textview value changes back to 0 and only shows the current deposited value.

tvBalanceAmt - textview that should show the updated balance.
allTotal - variable that takes the whole value user selection
balance - variable that should be updated after adding

If anyone can help me, it would be great as I've been really confused and have problems reading to understand.

Code:
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class DepositWithdrawInterface extends AppCompatActivity {

    TextView tvLabel2dollar;
    EditText etCustom2dollar;
    TextView tvLabel5dollar;
    EditText etCustom5dollar;
    TextView tvDisplayAmt;
    Button btnDep;
    TextView tvBalanceAmt;

    int total2;
    int total5;
    int total10;
    int total50;

    static int allTotal;
    private static int balance;

    public static int getBalance() {
        return balance;
    }



    @Override
    protected void onPause() {
        super.onPause();

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor prefEdit = prefs.edit();

        prefEdit.putInt("alltotal",allTotal);

        prefEdit.commit();

    }

    @Override
    protected void onResume() {
        super.onResume();

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);

        Integer intalltotal = prefs.getInt("alltotal",0);
        tvBalanceAmt.setText(Integer.toString(intalltotal));

    }


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

        btnDep = findViewById(R.id.buttonDeposit);


        tvBalanceAmt = findViewById(R.id.textViewAmt);
        tvDisplayAmt = findViewById(R.id.textViewDisplayAmt);

        tvLabel2dollar = findViewById(R.id.textViewLabel2);
        etCustom2dollar = findViewById(R.id.editText2);

        tvLabel5dollar = findViewById(R.id.textViewLabel5);
        etCustom5dollar = findViewById(R.id.editText5);



        //$2 note
        SeekBar mySeekBar = findViewById(R.id.seekBar2);


        mySeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {


            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                int value = 0;
                value = i;

                total2 = value * 2;

                String something = String.valueOf(i) + " x $2 = " + total2;

                tvLabel2dollar.setText(something);

                allTotal = total2 + total5;
                tvDisplayAmt.setText(String.valueOf(allTotal));


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        etCustom2dollar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.length() != 0) {

                    String input = etCustom2dollar.getText().toString();
                    int value = Integer.parseInt(input);


                    total2 = value * 2;

                    String something = input + " x $2 = " + total2;

                    tvLabel2dollar.setText(something);

                    allTotal = total2 + total5;
                    String allTotalString = Integer.toString(allTotal);
                    tvDisplayAmt.setText(allTotalString);


                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        //$5 note
        SeekBar mySeekBar5 = findViewById(R.id.seekBar5);


        mySeekBar5.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int value = 0;

            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                value = i;

                total5 = value * 5;

                String something = String.valueOf(i) + " x $5 = " + total5;

                tvLabel5dollar.setText(something);

                allTotal = total2 + total5;
                tvDisplayAmt.setText(String.valueOf(allTotal));


            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        etCustom5dollar.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if(charSequence.length() != 0) {

                    String input = etCustom5dollar.getText().toString();
                    int value = Integer.parseInt(input);


                    total5 = value * 5;

                    String something = input + " x $5 = " + total5;

                    tvLabel5dollar.setText(something);
                    allTotal = total2 + total5;
                    String allTotalString = Integer.toString(allTotal);
                    tvDisplayAmt.setText(allTotalString);

                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });


        btnDep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                balance = balance + allTotal;
                String strbalance = Integer.toString(balance);
                tvBalanceAmt.setText(strbalance);

            }
        });







        //$10 note



        //$50 note




    }
}
 
First thing is to check that your onPause() and onResume() methods are being called when you think they are. Put a breakpoint in those methods. Do they get hit?
 
I've set a breakpoint and how do I check if it gets hit?
I'm I think it does because the previous textview data is loaded
 
Back
Top Bottom