【问题标题】:Firestore Query whereEqualto and Document not getting the data from Firestore DBFirestore Query whereEqualto 和 Document 未从 Firestore DB 获取数据
【发布时间】:2021-09-29 13:30:15
【问题描述】:

我是使用 Android Studio 和 Firestore DB 的新手。 我正在尝试将几行文本从 firestore 加载到我的 android 应用程序中。我在调试时注意到 whereEqualto 字段名称的代码行没有读取所需的数据。我不知道在获取数据时哪里出错了。任何帮助,将不胜感激。这是我的代码。

ViewActivity.class

public class ViewActivity extends AppCompatActivity implements View.OnClickListener {
    private static String topic_name;
    ImageButton next, previous;
    TextView text_area;
    private Information infodb;
    public static List<String> mFragments;
    private Query query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewtext);

        FetchDetails();
        text_area= (TextView)findViewById(R.id.tf_text);
        next = (ImageButton) findViewById(R.id.next);
        next.setOnClickListener(this);
        previous = (ImageButton) findViewById(R.id.previous);
        previous.setOnClickListener(this);



    } 

public void FetchDetails(){
        FirestoreUtil.getCollection("Information Text").whereEqualTo("topic_name",topic_name).get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                if(task.isSuccessful()) {
                   
                    for (QueryDocumentSnapshot document : task.getResult()) {
                        Information ip = document.toObject(Information.class);
                        mFragments=(ip.getFragments());
                        text_area.setText(ip.getFragments().get(0));
                    }

                }
            }
        });
    }
@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.next:
                text_area.setText(infodb.getFragments().get(1));
                break;


            case R.id.previous:
               text_area.setText(infodb.getFragments().get(2));
                break;
        }
    }
}

信息类


public class Information {

    public List<String> fragments;
    private int topic_id;
    private String topic_name;
    private String game;
    private String link;
    private int level;


public Information() {

}

public Information(String topic_name, List<String> fragments,String game, String link, int level,int topic_id)
{
    this.fragments = fragments;
    this.topic_name= topic_name;
    this.game=game;
    this.topic_id=topic_id;
    this.level = level;
    this.link=link;


}

    public Information(List<String> fragments) {

    this.fragments=fragments;
    }

    public String getTopic_name() {return topic_name;}
    public void setTopic_name(String topic_name) {
        this.topic_name = topic_name;
    }

    public void setFragments(List<String> fragments) {
        this.fragments = fragments;
    }
    public List<String> getFragments() {
        return fragments;
    }

    public String getGame() { return game; }
    public void setGame(String game) { this.game = game; }

    public String getLink() { return link; }
    public void setLink(String link) { this.link = link; }

        public void setLevel(int level) {
        this.level = level;
    }
       public int getLevel() {
        return level;
    }

    public void setTopic_id(int topic_id) {this.topic_id=topic_id;}
    public int getTopic_id() {
        return topic_id;
    }

}

here is a screenshot of my data on firestore console

【问题讨论】:

  • 您的参考资料中topic_name 的值是多少?还请编辑您的问题并分享您的Information 课程的内容。
  • 嗨,Alex,我刚刚更新了 :) 感谢您抽出宝贵时间回复
  • .getCollection("Information Text") 返回什么,topic_name 在您的参考中的值是什么?
  • 问题是它根本没有进入这个集合。 topic_name 是我在同一个类中创建的字符串对象。
  • 刚刚编辑了整个文档以获得更清晰的图片

标签: android android-studio google-cloud-firestore


【解决方案1】:

查看您提供的代码后,我发现 ViewActivity.class 中的变量 private static topic_name 已声明但未在任何地方初始化。所以topic_name 默认情况下会有一个空值。这就是您在 whereEqualTo(“topic_name”,topic_name) 方法之后没有得到文档的原因。

因此,请使用 Firestore 集合中存在的值初始化 private static topic_name 变量。您可以按如下方式对其进行初始化 -

private static String topic_name = “Understanding PTSD”

您还使用FirestoreUtil.getCollection() 方法来获取集合。然而,在Firestore documentation 中,Firestore 数据库引用用于获取集合。 'db' Firestore 数据库引用可以按上述here 进行初始化。

所以我建议您在 ViewActivity.class 级别初始化 Firestore 数据库引用,如下所示 -

FirebaseFirestore db = FirebaseFirestore.getInstance();

然后使用这个引用来获取集合如下-

db.collection("Information Text").whereEqualTo("topic_name",topic_name).get()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    相关资源
    最近更新 更多