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
相关产品推荐
相关产品推荐

