Home > Mobile >  How to get a field and update a textview from firestore?
How to get a field and update a textview from firestore?

Time:10-12

I am trying to get a value from Firestore but I am getting null every time. Can someone tell me what wrong I am doing? This is my Firestore collections and documents.FirestoreCollections

    DocumentReference docRef =  dbase.collection("newsImage").document(
            "cqmpNF45IZsHDSx9hSuq");
    colref.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
        @Override
        public void onComplete(@NonNull @NotNull Task<DocumentSnapshot> task) {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    str = document.getString("imgNews");

                    tv.setText(str);

                }else {
                    Log.d("docv", "No such document");
                }
            } else {
                Log.d("docv", "get failed with ", task.getException());
            }

        }
    });

CodePudding user response:

Most likely the problem comes from the following lines of code:

String newsText= ipustr;
tv.setText(sectorText);

Where you try to assign a value that doesn't come from the database. So most probably you should do something like this:

str = document.getString("imgNews");
tv.setText(str);

Or even simples:

tv.setText(document.getString("imgNews"));

Edit:

The name of the filed is incorrect, so please use:

tv.setText(document.getString("imgnews"));
  • Related