Sum fields of a Firebase Bank

Asked

Viewed 185 times

0

I need to add the fields of my bank Firebase, that is the field Receita that is inside the collection Dados follows the picture below:

inserir a descrição da imagem aqui

I am using the following code to make a simple read, sets to read the recipe field that is within the collection Dados in the document 3954d8bf-8796-4955-9d7c-0f245dcbbfdf

public class TelaCentral extends AppCompatActivity {

    FirebaseAuth firebaseAuth;
    FirebaseUser firebaseUser;
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    DocumentReference dados =  db.document("Dados/3954d8bf-8796-4955-9d7c-0f245dcbbfdf");

    TextView emailExibe,campoReceita;
    Button buttonSair;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_telacentral);

        firebaseAuth = FirebaseAuth.getInstance();
        firebaseUser = firebaseAuth.getCurrentUser();

        Leitura();
       }



    // LEITURA
    private void Leitura(){
        dados.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
            @Override
            public void onSuccess(DocumentSnapshot documentSnapshot) {
                if (documentSnapshot.exists()){
                    String recei = documentSnapshot.getDouble("Receita").toString();
                    campoReceita.setText(recei);

                }
            }
        });
    }    

}

However, I would like to go through all the Documentos that are in the collection Dados and add the field Receita to display to the user, how could it do this?

  • Are you making a mistake? What’s going on? Only the first is returning?

  • pq recipe is a string? should be decimal

1 answer

0

To add up there are two ways one going on all the items and add up

db.collection("Dados")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                Double valor = 0.0
                for (QueryDocumentSnapshot document : task.getResult()) {
                   valor += documentSnapshot.getDouble("Receita")
                }
                campoReceita.setText(valor.toString());
            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

Or each time you add the value

db.collection("Dados")
    .document("soma")
    .get()
    .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            if (task.isSuccessful()) {
                Double valor = 0.0
               if (documentSnapshot.exists()){
                     valor = documentSnapshot.getDouble("Receita")
                }
                 valor += novoitem.valor
                  db.collection("Dados").document("soma")
                    .set(valor)
                    .addOnSuccessListener(new OnSuccessListener<Void>() {
                        @Override
                        public void onSuccess(Void aVoid) {
                            Log.d(TAG, "DocumentSnapshot successfully written!");
                        }
                    })
                    .addOnFailureListener(new OnFailureListener() {
                        @Override
                        public void onFailure(@NonNull Exception e) {
                            Log.w(TAG, "Error writing document", e);
                        }
                    });

            } else {
                Log.d(TAG, "Error getting documents: ", task.getException());
            }
        }
    });

Browser other questions tagged

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