Display a value of Firestore in a TextView - Android Studio

0

I have a collection stored in Firestore, which is called "Score", there I have saved different values (all are int).

What I want is to show the values in another simple activity.

I tried the following code:

public void Resultado(View view) {

    noteref.get()
            .addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()){
                        String resultado = documentSnapshot.getString("Uid");

                        resul.setText(resultado);
                    } else {
                        Toast.makeText(ResultadoQ1.this, "No hay datos", Toast.LENGTH_SHORT).show();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {

        }
    });
}

The problem is that when you run the activity, only the "The app has problems" warning comes out.

I would greatly appreciate the help

    
asked by Milagros 08.10.2018 в 03:06
source

1 answer

0

You could try the following

DocumentReference user = db.collection("puntaje").document(idDelPost);
        user.get().addOnCompleteListener(new OnCompleteListener < DocumentSnapshot > () {
                @Override
                public void onComplete(@NonNull Task < DocumentSnapshot > task) {
                    if (task.isSuccessful()) {
                        DocumentSnapshot doc = task.getResult();
                        StringBuilder fields = new StringBuilder("");
                        fields.append("Score01: ").append(doc.get("score01"));
                        fields.append("\n Score02: ").append(doc.get("score02"));
                        fields.append("\n Score03: ").append(doc.get("score03"));
                        resul.setText(fields.toString());
                    }
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                }
            });

where idDelPost can be your mAuth.getCurrentUser().getUid() or what you have generated for that id that contains the data. Or you can also replace it directly by the document you want to obtain, in this case 498ZBk70KEjlL1v1Zkfp (you would be .document("498ZBk70KEjlL1v1Zkfp"); )

    
answered by 08.10.2018 в 04:09