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 methodsetText
to display onTextView
.– Valdeir Psr