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

为何仅显示新增数据而非全部?Firebase数据展示问题求助

Firebase数据展示异常修复问题

当前仅能显示新增的数据,无法展示全部数据:

  • 现状:仅显示最新新增的单条数据
  • 期望效果:展示所有用户的数据列表

相关代码:

FirebaseUser firebaseKullanici = yetki.getCurrentUser();
String kullaniciId = firebaseKullanici.getUid();
yol = FirebaseDatabase.getInstance().getReference().child("Kullanıcılar").child("KullaniciId");
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("id", kullaniciId);
hashMap.put("kullaniciAdi", kullaniciAdi.toLowerCase());
hashMap.put("adsoyad", adsoyad);
hashMap.put("bio", "");
hashMap.put("resimurl", "https://firebasestorage.googleapis.com/v0/b/uygulama-4683b.appspot.com/o/placeholder-user-scaled.jpg?alt=media&token=29e7ea5c-3d37-4d06-a119-badbf3fe944b");              

解决方法:

  1. 修正数据存储路径
    代码里child("KullaniciId")是固定字符串,所有用户数据都会写入到同一个节点下,新数据直接覆盖旧数据,导致只能看到最后新增的条目。把它替换为变量kullaniciId,让每个用户的数据存储在以自身UID命名的独立节点中:

    yol = FirebaseDatabase.getInstance().getReference().child("Kullanıcılar").child(kullaniciId);
    
  2. 调整数据读取逻辑
    读取数据时要监听Kullanıcılar整个节点,而非单个固定节点。示例代码如下,通过遍历所有子节点加载全部用户数据:

    DatabaseReference kullaniciRef = FirebaseDatabase.getInstance().getReference().child("Kullanıcılar");
    kullaniciRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            kullaniciListesi.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Kullanici kullanici = snapshot.getValue(Kullanici.class);
                kullaniciListesi.add(kullanici);
            }
            adapter.notifyDataSetChanged();
        }
    
        @Override
        public void onCancelled(DatabaseError databaseError) {
            // 处理错误情况
        }
    });
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 08:25:18