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

如何在FirebaseRecyclerOption中仅获取并展示当前用户数据

解决方案

要实现仅展示当前用户的「sections」数据,核心是精准定位到当前用户的sections节点并构建对应的Firebase查询,具体步骤如下:

  1. 获取当前登录用户的UID
    通过Firebase Auth拿到当前用户的唯一标识,这是过滤数据的核心依据:

    String currentUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
    
  2. 构建指向当前用户sections的数据库引用
    根据你的数据库结构(用户节点下嵌套sections),直接定位到该用户的sections节点:

    DatabaseReference userSectionsRef = FirebaseDatabase.getInstance()
            .getReference("users")
            .child(currentUserId)
            .child("sections");
    
  3. 配置FirebaseRecyclerOption
    基于上述引用构建查询,确保只加载当前用户的sections数据:

    FirebaseRecyclerOptions<SectionModel> options = new FirebaseRecyclerOptions.Builder<SectionModel>()
            .setQuery(userSectionsRef, SectionModel.class)
            .build();
    

    注意:这里的SectionModel需要对应你sections节点下的单条数据结构(而非之前的User模型),请确保模型类的字段与sections子节点的键名完全匹配。

  4. 调整Adapter与ViewHolder
    你的RecyclerView Adapter需要适配SectionModel,ViewHolder也要修改绑定逻辑,展示sections的具体字段(比如标题、内容等),而非原User模型的字段。

  5. 验证数据库权限
    确保Firebase数据库规则允许当前用户仅访问自己的sections数据,示例规则如下:

    {
      "rules": {
        "users": {
          "$uid": {
            "sections": {
              ".read": "$uid === auth.uid",
              ".write": "$uid === auth.uid"
            }
          }
        }
      }
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 01:27:09