简体   繁体   English

如何从firestore获取字段并更新textview?

[英]How to get a field and update a textview from firestore?

I am trying to get a value from Firestore but I am getting null every time.我试图从 Firestore 获取值,但每次都为 null。 Can someone tell me what wrong I am doing?有人能告诉我我做错了什么吗? This is my Firestore collections and documents.这是我的 Firestore 集合和文档。

Firestore集合

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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM