Pick product prices in an array, add them and display the sum total in a TextView

1
Hello, guys, so I'm having a hard time solving a problem here, I'm using a MultiAutoCompleteTextView to get products and their prices in the app's Firebase database, I can already display all the products inside the autocomplete, however, I I would like to take the price of each product, add all and present the total in a TextView, however I have no idea where to start. I do not know if I should get every element of the ArrayList and add, or should I do otherwise. If you can help me, I'll 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) {

        }

    });
    
asked by anonymous 16.01.2018 / 05:24

1 answer

1

Hand you are using an ArrayList of Strings (List), and concatenating values, then you will not be able to retrieve the arrays. So the first thing to do is to create a class of objects with the elements that 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 List<String> , it 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];
        }

In this way you can retrieve the values where you want with the gets, eg:

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

You can use them wherever you want, concatenate to display in the textview, or even double to do calculations, eg:

double total = Double.parseDouble(produto1.get(0).getValor()) * Double.parseDouble(produto1.get(1).getValor())
    
16.01.2018 / 12:42