繁体   English   中英

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

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

我试图从 Firestore 获取值,但每次都为 null。 有人能告诉我我做错了什么吗? 这是我的 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());
    }
});

问题很可能来自以下代码行:

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

您尝试分配不是来自数据库的值的地方。 所以很可能你应该做这样的事情:

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

甚至是简单的:

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

编辑:

提交的名称不正确,请使用:

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

暂无
暂无

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

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