【问题标题】:Retrieve all data under Uid Firebase Android检索 Uid Firebase Android 下的所有数据
【发布时间】:2016-11-09 09:50:36
【问题描述】:

大家好,我是 Firebase 的新手,我正在尝试检索 Uid 下的所有数据。 我这样存储它们:

mDatabase=FirebaseDatabase.getInstance().getReference().child("Data").child(auth.getCurrentUser().getUid());
//some other code here
 DatabaseReference newProduct =mDatabase.child(category).child("Product").push();
 newProduct.child("productname").setValue(name);
 newProduct.child("date").setValue(date);

我的问题是如何检索 userUid 类别下的所有数据?我需要.child(*).child("Products") 之类的东西吗?

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    您需要做的是使用侦听器来检索该数据,类似于

     mDatabase.child(category).child("Product").addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                     String name = (String) dataSnapshot.child("productname").getValue;
                     String date = (String) dataSnapshot.child("date").getValue;
    
    
                    }
    
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
    
                    }
                });
    

    你去吧,现在你把所有的数据都拉成字符串了。

    另外,我会建议,而不是

    auth.getCurrentUser().getUid()
    

    使用

    String uid = (String) auth.getCurrentUser().getUid();
    

    只需将 uid 放在那里,这样你的代码就更简洁了,每次你需要引用 uid 时,你都有一个字符串。

    【讨论】:

    • 应该是:String uid = FirebaseAuth.getInstance().getCurrentUser().getUid(); ?
    • 是一样的。他是这样做的: FirebaseAuth auth = FirebaseAuth.getInstance(); String uid = (String) auth.getCurrentUser().getUid();
    猜你喜欢
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多