【问题标题】:I added a map as a field in my Firestore DB document with a sub field inside, and I am facing issues to my RecyclerView我在我的 Firestore DB 文档中添加了一个地图作为一个字段,里面有一个子字段,我的 RecyclerView 遇到了问题
【发布时间】:2021-12-23 18:17:59
【问题描述】:

当我在查询中包含添加的地图字段时,我的 RecyclerView 在显示我的应用程序的候选者时遇到了问题。我还为此查询创建了一个索引,但我的 RecyclerView 仍然没有任何结果。

这是更新后的查询代码。如果编码有问题,请通知我。

FirebaseUser user = mAuth.getCurrentUser();
    String voterID = user.getUid();
    Query query = notebookRef.whereEqualTo("registrationStatus", "Accepted").whereEqualTo( voterID, false).orderBy("candidateFullName", Query.Direction.ASCENDING);

    FirestoreRecyclerOptions<VoterCandidateItem> options = new FirestoreRecyclerOptions.Builder<VoterCandidateItem>()
            .setQuery(query, VoterCandidateItem.class)
            .build();

    adapter = new VoterCandidateAdapter(options);

    recyclerView = findViewById(R.id.candidateList);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
    recyclerView.setItemAnimator(null);

这是 Firestore DB 的更新截图:

这是索引的更新截图:

【问题讨论】:

  • .....whereEqualTo(voterID, false)...voterID 在哪里?没看到
  • @SusanMustafa 我的错。我现在将它包含在上面的代码中

标签: java android google-cloud-firestore android-recyclerview


【解决方案1】:

以下查询:

Query query = notebookRef
        .whereEqualTo("registrationStatus", "Accepted")
        .whereEqualTo(voterID, false)
        .orderBy("candidateFullName", Query.Direction.ASCENDING);

需要index,否则,它将不起作用。如果您使用的是 Android Studio,您会在 logcat 中找到如下所示的错误消息:

FAILED_PRECONDITION: The query requires an index. You can create it here: https://console.firebase.google.com/v1/r/project/...

可以直接在Firebase Console 中创建所需的查询,也可以通过单击该链接来创建。如果点击不起作用,只需将 URL 复制并粘贴到网络浏览器中,系统就会自动为您创建索引。

如果选择控制台,记得设置:

集合:候选人 字段:registrationStatus 升序voterID:升序

【讨论】:

  • 我已经通过我在 logcat 中看到的链接创建了该索引,但我的 RecyclerView 仍然没有显示满足查询的候选人。
  • 索引错误。检查我编辑的答案。除此之外,voterID 应该 NOT 在其中包含.。因为它将被视为地图内的地图,地图内的地图,依此类推。您应该使用固定名称,而不是动态名称。
  • String voterID 包含当前用户的电子邮件。如何将电子邮件用作 .whereEqualTo 的字段名称?
  • 如果需要,您应该使用用户的 UID,因为它不包含任何 .。或者更好的是,你应该使用一个单独的字段,直接在文档内部调用voterID,在地图内部not,然后创建相应的索引。试一试,告诉我它是否有效。
  • 我去看看,如果我知道答案,我会写信给你。
猜你喜欢
  • 2021-09-03
  • 1970-01-01
  • 2023-01-09
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 2020-02-22
  • 1970-01-01
  • 2021-04-20
相关资源
最近更新 更多