Dynamic Total according to Android NumberPicker

0

I'm using NumberPicker so that the user selects the quantity. Each numberpicker has a total that already appears on the screen according to the amount selected by the user.

Example: NumberPicker 1 = qnt 3 = $ 15.00

NumberPicker 2 = qnt 2 = $ 10.00

NumberPicker 3 = qnt 4 = $ 8.00

The total of each item is dynamic, so it changes the quantity already shows the total value in front.

I am stuck in the grand total. This grand total would pick up all the totals and add up and return a dynamic overall total.

Numberpickers listeners below

np_Garrafa.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int numEscolhido) {

            qntGarrafa = numEscolhido;
            total_Garrafa = qntGarrafa * 10;
            txt_totalGarrafa.setText("R$ " + total_Garrafa);
            Log.i("garrafa", String.valueOf(qntGarrafa));
        }
    });

    np_Garrafao10.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int numEscolhido) {

            qntGarrafao10 = numEscolhido;
            total_Garrafao10 = qntGarrafao10 * 5;
            txt_totalGarrafao10.setText("R$ " + total_Garrafao10);
            Log.i("garrafa10", String.valueOf(qntGarrafao10));

        }
    });

    np_Garrafao20.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int numEscolhido) {

            qntGarrafao20 = numEscolhido;
            total_Garrafao20 = qntGarrafao20 * 10;
            txt_totalGarrafao20.setText("R$ " + total_Garrafao20);
            Log.i("garrafa20", String.valueOf(qntGarrafao20));

        }
    });

I tried to put in each listener a variable that would receive the total of each item and display in the overall total textview. But if user decreases the amount it still keeps adding up.

    
asked by anonymous 05.08.2017 / 20:46

1 answer

1

You could try something like this:

float totalGeral = 0.0f;
// cria uma variável total geral
np_Garrafa.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int numEscolhido) {
        qntGarrafa = numEscolhido;
        total_Garrafa = qntGarrafa * 10;
        totalGeral+=total_Garrafa;
        // soma o q tem na garrafa ao total geral
        txt_totalGarrafa.setText("R$ " + total_Garrafa);
        Log.i("garrafa", String.valueOf(qntGarrafa));
    }
});

np_Garrafao10.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int numEscolhido) {
        qntGarrafao10 = numEscolhido;
        total_Garrafao10 = qntGarrafao10 * 5;
        totalGeral+=total_Garrafa10;
        // soma o q jah tem no totalGeral + garrafa10
        txt_totalGarrafao10.setText("R$ " + total_Garrafao10);
        Log.i("garrafa10", String.valueOf(qntGarrafao10));

    }
});

np_Garrafao20.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
    @Override
    public void onValueChange(NumberPicker numberPicker, int i, int numEscolhido) {

        qntGarrafao20 = numEscolhido;
        total_Garrafao20 = qntGarrafao20 * 10;
        totalGeral+=total_Garrafa20;
        // soma o q jah tem no toalGeral + o que tem na garrafa20
        txt_totalGarrafao20.setText("R$ " + total_Garrafao20);
        Log.i("garrafa20", String.valueOf(qntGarrafao20));

    }
});
// dai
txt_totalGeral.SetText("R$ " + totalGeral);

I hope it works.

warns that it worked out

    
05.08.2017 / 23:12