Dynamic total according to Android Numberpicker

Asked

Viewed 178 times

0

I am 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 = R$ 15,00

Numberpicker 2 = qnt 2 = R$10,00

Numberpicker 3 = qnt 4 = R$8,00

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

I am stuck in the overall total. That overall total would take all the totals and sum and return a dynamic overall total.

Below the listeners of Numberpickers

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 textview. But if user decrease the amount it still continues adding.

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);

hope it works.

Let me know if it works

  • I used what you put and applied in another method. As soon as the user adds to the cart he makes the total sum and already displays, it became much more practical. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.