how do I see the total

Asked

Viewed 36 times

-2

I’m creating a college shopping app, I need that when I press the add button it shows the value of the specific product in total and that it adds together the following values when pressed the add button more times but I do not know how to do someone could help me?

inserir a descrição da imagem aqui

public class Mainactivity extends Appcompatactivity {

private Button add_btn1, add_btn2, add_btn3, add_btn4, add_btn5, add_btn6;
private Button remove_btn1, remove_btn2, remove_btn3, remove_btn4, remove_btn5, remove_btn6;
private TextView count1, count2, count3, count4, count5, count6;
private TextView total_count;


private int contador1 = 0;
private int contador2 = 0;
private int contador3 = 0;
private int contador4 = 0;
private int contador5 = 0;
private int contador6 = 0;


@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    count1 = (TextView) findViewById(R.id.count1);
    count2 = (TextView) findViewById(R.id.count2);
    count3 = (TextView) findViewById(R.id.count3);
    count4 = (TextView) findViewById(R.id.count4);
    count5 = (TextView) findViewById(R.id.count5);
    count6 = (TextView) findViewById(R.id.count6);

    add_btn1 = (Button) findViewById(R.id.add_btn1);
    add_btn2 = (Button) findViewById(R.id.add_btn2);
    add_btn3 = (Button) findViewById(R.id.add_btn3);
    add_btn4 = (Button) findViewById(R.id.add_btn4);
    add_btn5 = (Button) findViewById(R.id.add_btn5);
    add_btn6 = (Button) findViewById(R.id.add_btn6);

    total_count = (TextView) findViewById(R.id.total_count);

    add_btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador1++;
            count1.setText("" + contador1);
        }
    });
    remove_btn1 = (Button) findViewById(R.id.remove_btn1);

    remove_btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador1--;
            count1.setText("" + contador1);
        }
    });

    add_btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador2++;
            count2.setText("" + contador2);
        }
    });
    remove_btn2 = (Button) findViewById(R.id.remove_btn2);

    remove_btn2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador2--;
            count2.setText("" + contador2);
        }
    });

    add_btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador3++;
            count3.setText("" + contador3);
        }
    });
    remove_btn3 = (Button) findViewById(R.id.remove_btn3);

    remove_btn3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador3--;
            count3.setText("" + contador3);
        }
    });

    add_btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador4++;
            count4.setText("" + contador4);
        }
    });
    remove_btn4 = (Button) findViewById(R.id.remove_btn4);

    remove_btn4.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador4--;
            count4.setText("" + contador4);
        }
    });

    add_btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador5++;
            count5.setText("" + contador5);
        }
    });
    remove_btn5 = (Button) findViewById(R.id.remove_btn5);

    remove_btn5.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador5--;
            count5.setText("" + contador5);
        }
    });

    add_btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador6++;
            count6.setText("" + contador6);
        }
    });
    remove_btn6 = (Button) findViewById(R.id.remove_btn6);

    remove_btn6.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            contador6--;
            count6.setText("" + contador6);
        }
    });


}

1 answer

0


Create a variable total.

For each click on the buttons that increases the amount, put this code total += contador1 * preco. Change the name of the counter1 to the correct variable.

For each click on buttons that decrease the amount, put this code total -= contador1 * preco. Change the name of the counter1 to the correct variable.

Create a variable price for each product.

//private int contador6 = 0;
private float total = 0;

add_btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        contador1++;
        total += contador1 * preco //precisa criar a variavel preço para cada produto
        count1.setText("" + contador1);
        total_count.setText("" + total);
    }
});

Browser other questions tagged

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