如何从一个userID Firebase Android读取/查询数据

时间:2017-01-25 10:15:14

标签: android firebase firebase-realtime-database

我想检索给定用户ID的数据,我将其作为值存储在每个用户下的数据库中。我的数据库结构如下所示:

user: {
    UserID: { 
       uniqueId: {
         Name: ""
         Email: ""
         userId: ""
    }

    UserID: { 
       uniqueId: {
         Name: ""
         Email: ""
         userId: ""
    }
}

我试过了,但它似乎没有效果,因为它没有在应用上显示任何内容:

FirebaseUser user = mAuth.getCurrentUser();
df.child("UsersTable").orderByChild("userid").equalTo(user.getUid())).addChildEventListener

即使我尝试提供如下特定值,它仍然无法从firebase中读取: -------------更新:

我想从userID获取所有数据。所以例如,我想从fmXWU324VHdDb8v6h7gvNjRojnu33中检索所有信息,这些信息将是名称,电子邮件和用户ID。

UserTable
{
  "fmXWU324VHdDb8v6h7gvNjRojnu33" : {
    "-KbJtw467nl6p253HZ537" : {
      "Name" : "name1",
      "Email" : "something1@something.com",
      "userid" : "fmXWU324VHdDb8v6h7gvNjRojnu33"
    }
  },
  "pJtC45fW0MMi352UiPWnWdIS7h88" : {
    "-Kb012ls9iMnzEL723o9I" : {
      "Name" : "name2",
      "Email" : "something2@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    },
    "-Kb0aq0q25FJq38256SrC" : {
      "Name" : "name3",
      "Email" : "something3@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    },
    "-Kb0atopfK64F6jy124Qi1u" : {
      "Name" : "name3",
      "Email" : "something1@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
  }
}

更新2:

     DatabaseReference df= FirebaseDatabase.getInstance()
                        .getReference("user").child(getCurrentUser().getUid());   

df.addChildEventListener(new ChildEventListener() {
                    @Override
                    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                        get(dataSnapshot);
                    }
            ....
            ...

更新3:

我假设根据我要查找的内容(在定义的userId下获取所有数据),查询应如下所示:

df.child("UserTable").child(user.getUid())).addChildEventListener(new ChildEventListener() 

但是,这会引发此错误:

Can't convert object of type java.lang.String to type com.android.example.UserTable

4 个答案:

答案 0 :(得分:1)

尝试使用addChildEventListener和查询,如下所示..

mReference.child("user").orderByChild("userId").equalTo(user.getUid()).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        //deal with data object
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

或使用addValueEventListener

mReference.child("user").orderByChild("userId").equalTo(user.getUid()).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

             //deal with data object

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            //toastMsg(databaseError.getMessage());
            Log.e(TAG, "onCancelled: " + databaseError.getMessage());
        }
    });

答案 1 :(得分:0)

请从下面的代码中了解一下。

订购对象:

Order objects

DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance()
                .getReference();
DatabaseReference node = mDatabaseReference.child("order")

node.orderByChild("timestamp").equalTo("-KZFQLVAv_kARdfnNnib")
                   .addValueEventListener(mValueEventListener);

private ValueEventListener mValueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            Log.i(TAG, "onDataChange: " + snapshot.getValue());
            // Your code
        }

        @Override
        public void onCancelled(DatabaseError error) {
            Log.w(TAG, "onCancelled: " + error.getMessage());
        }
    };

这返回第一个订单对象。希望你明白这一点谢谢! :)

答案 2 :(得分:0)

使用我在我的应用程序中使用的以下代码,它可以正常工作 -

public static void storagesSetup(final Context context){

        if(!hasSignedInUser()) {
            **your business logic here**
            return;
        }

        // Write a message to the database
        final DatabaseReference databaseRef = FirebaseDatabase.getInstance()
                .getReference("cart").child(getCurrentUser().getUid());

        // Read from the database
        databaseRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.

                **put your business logic here**

            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });
    }

答案 3 :(得分:0)

Firebase数据库查询直接检查您在其上执行的位置下的子节点。如果要对这些节点的子属性进行排序(即使用orderByChild()),则该属性必须存在于每个子节点下的固定路径中。

在您的情况下,您有两个嵌套的动态子项,这意味着您:

  • 可以向已知用户查询按分数排序的结果
  • 无法在所有用户之间查询按分数排序的结果

如果要允许后一个查询,则需要更改数据模型以允许它。例如,通过保留所有玩家的所有结果列表:

ResultsTable:{
    "-KbJtw467nl6p253HZ537" : {
      "Name" : "name1",
      "Email" : "something1@something.com",
      "userid" : "fmXWU324VHdDb8v6h7gvNjRojnu33"
    },
    "-Kb012ls9iMnzEL723o9I" : {
      "Name" : "name2",
      "Email" : "something2@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    },
    "-Kb0aq0q25FJq38256SrC" : {
      "Name" : "name3",
      "Email" : "something3@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    },
    "-Kb0atopfK64F6jy124Qi1u" : {
      "Name" : "name3",
      "Email" : "something1@something.com",
      "userid" : "pJtC45fW0MMi352UiPWnWdIS7h88"
    }
}