Mathematical operation in Recyclerview

Asked

Viewed 344 times

1

I want to know how to add up certain values of a Recyclerview.. in case it will be the model.getQntd(), I need to add all your respective results.

UPDATING

I managed to count the problem now is that when it happens the change in the server (firebase), the sum increases a new number instead of modifying... Example I have 2 values on the example server:.

} teste1: qntd:10 } ...

} teste2: qntd:10 }

if I change the qntd of teste1 for 11 , the result will be, 10+10+11=31 and not 21, since the 10+10 had already been added to the list and the 11 comes as a new datum... if I leave 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);

    }
    }
  • 2

    Take a look at this: https://stackoverflow.com/a/43089074/1377664

  • How the data for the RecyclerView ? Include the code for that aid part

  • So I updated it.

  • Thanks @DVD I had to change my code but it was right.

  • The total is a fixed field on your screen or will be another line (the last) of the data displayed?

  • is a fixed field, not part of recyclerview.... that’s what you asked?

  • this link that the DVD passed me worked, the problem is when it updates a data in the server.. type I have two 10 in firebase if I change a 10 to 11, instead of the result being 21 , turns 31, why that happen I know...I just don’t know how to solve

  • Your code is badly formatted and it seems you have not provided all the relevant code. Please inform us the code you are using to calculate the total.

Show 3 more comments
No answers

Browser other questions tagged

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