You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firestore新增Map字段后查询时RecyclerView无数据如何解决?

问题原因

你当前查询语句的筛选条件字段名写法错误:你新增的选民ID布尔值是voted Map类型字段下的子字段,Firestore 中查询嵌套 Map 的子字段需要使用点表示法拼接父字段名与子字段名,不能直接将子字段名作为根级字段查询。

你原代码中whereEqualTo( voterID, false)会去匹配文档根级和voterID同名的字段,而该字段实际不存在,因此查询无返回结果,RecyclerView自然无内容展示。

修正后的代码

FirebaseUser user = mAuth.getCurrentUser();
String voterID = user.getUid();
// 修正点:用点表示法指定Map下的子字段
Query query = notebookRef.whereEqualTo("registrationStatus", "Accepted")
        .whereEqualTo( "voted." + 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);

额外检查项

  • 确认你使用的FirestoreRecyclerAdapter在页面生命周期的onStart方法中调用了adapter.startListening(),在onStop方法中调用了adapter.stopListening(),适配器默认不会自动开启数据监听。
  • 确认索引字段匹配修正后的查询条件:索引需要包含registrationStatus、voted.对应选民ID、candidateFullName三个字段,状态为已生效。

内容的提问来源于stack exchange,提问作者HeroreH29

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 19:06:11