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.
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());
}
});
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"));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.