I want to know how to add certain values from a RecyclerView.
in case it will be model.getQntd()
, I need to add all their respective results.
UPDATE
I've been able to count the problem now is that when there happens a change in the server (firebase), the sum increments a new number instead of modifying ... Example its I have 2 values in the example server :.
}
teste1:
qntd:10
}
...
}
teste2:
qntd:10
}
If I change the qntd
from teste1
to 11
, the result will be, 10 + 10 + 11 = 31 and not 21
, since 10+10
has already been added to the list and 11
comes as a new data ... if I go out and return to activity the result will be 21 ...
Thank you in advance.
mFireAdapter = new FirebaseRecyclerAdapter<Afiliado, BlogViewHolder>(
Afiliado.class,
R.layout.row_afiliados,
BlogViewHolder.class,
mQuery
) {
int total =0;
int total1 =0;
@Override
protected void populateViewHolder(final BlogViewHolder viewHolder, final Afiliado model, final int position) {
final String key = getRef(position).getKey();
final int teste = model.getQntd();
viewHolder.mNome.setText(model.getNome());
viewHolder.mQntd.setText(String.valueOf(model.getQntd()));
viewHolder.mRende.setText(String.valueOf(model.getQntd()/5));
total += teste;
total1 += teste/5;
mTotal1.setText(String.valueOf(total));
mTotal2.setText(String.valueOf(total1));
Log.i("Resultado", String.valueOf(total));
mData.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String cod = (String) dataSnapshot.child(users).child(mUser.getUid()).child("cod").getValue();
for(DataSnapshot snap : dataSnapshot.child(afiliados).child(cod).getChildren());
String resultado = dataSnapshot.child(afiliados).child(cod).getChildrenCount()+"";
String maxValue = (String) dataSnapshot.child(users).child(mUser.getUid()).child("maxAfiliados").getValue();
int valor1 = Integer.parseInt(resultado);
int resultado1 = valor1+total1;
mMeus.setText(String.valueOf(valor1));
mBruto.setText(String.valueOf("Resultado: "+resultado1));
mCircleView.setValue(resultado1);
mCircleView.setMaxValue(Float.parseFloat(maxValue));
mCod.setText(cod);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
//total+=mFireAdapter.get(position).getQntd();
//f(position==mQuestLIst.size()-1)
}
};
mFireAdapter.notifyDataSetChanged();
mRecycler.setAdapter(mFireAdapter);
// Update UI
//firebaseRecycleAdapter.notifyDataSetChanged();
}
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
private TextView mNome,mQntd,mRende;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
mNome = mView.findViewById(R.id.nome);
mQntd = mView.findViewById(R.id.qntd);
mRende = mView.findViewById(R.id.rende);
}
}