Pick up product prices in an array, add them up and present the total sum in a Textview

Asked

Viewed 944 times

1

Hello, guys, so, I’m having a hard time solving a problem here, I’m using a Multiautocompletetextview to pick up products and their prices in the app’s Firebase database, however, I would like to take the price of each product, add them all up and present the total in a Textview, but I have no idea where to start. I do not know if I should take each element of Arraylist and add, or if I should do otherwise. If you can help me, I will be very grateful.

  firebase.child("Produtos").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // Is better to use a List, because you don't know the size
            // of the iterator returned by dataSnapshot.getChildren() to
            // initialize the array
            final List<String> produto1 = new ArrayList<>();

            for (final DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
                produto = areaSnapshot.child("nome").getValue(String.class);
                valor = areaSnapshot.child("valor").getValue(String.class);
                produto1.add("Serviço: " + produto + " - Valor: R$" + valor);
                totalParaPagar = (TextView) findViewById(R.id.textViewTotal);
                int [] an = new int[0];
            }


            produtoRamissao = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView);
            ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(OrdemServicoActivity.this, android.R.layout.simple_list_item_1, produto1);
            areasAdapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
            produtoRamissao.setAdapter(areasAdapter);
            produtoRamissao.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }

    });
  • Create a global variable and add the sum into the for. soma += Integer.parseInt(valor);. Then use the method setText to display on TextView.

1 answer

1

Bro you’re using a String Arraylist (List), and concatenating values, so you won’t be able to redeem the tap values. Then the first thing to do is create a class of objects with the elements you want to insert into the list, for example:

public class Produtos{

    private String nome;
    private String valor;

    public String getNome() { return nome; }

    public void setNome(String nome) { this.nome = nome; }

    public String getValor() { return valor; }

    public void setValor(String valor) { this.valor = valor; }
}

Now instead of creating the List<String>, will create a List<Produtos>, and implement as follows:

List<Produtos> produto1 = new ArrayList<>();

        for (final DataSnapshot areaSnapshot: dataSnapshot.getChildren()) {
            produto = areaSnapshot.child("nome").getValue(String.class);
            valor = areaSnapshot.child("valor").getValue(String.class);

            Produtos itemProduto = new Produtos();
            itemProduto.setNome(produto);
            itemProduto.setValor(valor);
            produto1.add(itemProduto);

            totalParaPagar = (TextView) findViewById(R.id.textViewTotal);
            int [] an = new int[0];
        }

This way you can redeem the values where you want with the gets, ex:

produto1.get(posicao).getNome();
produto1.get(posicao).getValor();

You can use them anywhere, concatenate to display in the textview, or even turn into double to do calculations, ex:

double total = Double.parseDouble(produto1.get(0).getValor()) * Double.parseDouble(produto1.get(1).getValor())
  • @Brian, accept this answer if it helped you, so others know that your problem has been solved.

  • Brenddon then helped to some extent because I couldn’t achieve success. It helped me learn more about, but I didn’t solve the problem.

Browser other questions tagged

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