从firebase获取Uid

时间:2016-10-30 13:36:26

标签: android firebase firebase-realtime-database

我正试图从firebase找到uid。假设我的数据库中有3个用户,我想找到第3个用户uid。

photo of firebase dashbord

这是json formate女巫刚刚从firebase出口:

{
  "-KVKrXu70Ziw0iRw46l5" : {
    "age" : 20,
    "emailId" : "ameerhamza",
    "name" : "hamza"
  },
  "-KVKrkVy5OojpsSUEI24" : {
    "age" : 20,
    "emailId" : "ameerhamza",
    "name" : "hamza"
  },
  "-KVKrkzXy9duyiZq5bM0" : {
    "age" : 21,
    "emailId" : "ameerhamzaaaa",
    "name" : "hamza"
  },
  "-KVKrl5Ax7ptodMYywzg" : {
    "age" : 22,
    "emailId" : "ameerhamza",
    "name" : "hamza"
  },
  "-KVKrr7E6b4-3x6tuwOX" : {
    "age" : 23,
    "emailId" : "ameerhamza",
    "name" : "hamza"
  },
  "-KVKruES01regClm6tWW" : {
    "age" : 24,
    "emailId" : "ameerhamza",
    "name" : "hamza"
  }
}

以下是我所做的代码。但它不适合我。

 final FirebaseDatabase database = FirebaseDatabase.getInstance();
        myRef = database.getReference();
       DatabaseReference newRef = myRef.push();
          i++;
                    Student s = new Student("ameerhamza",i,"hamza");
                    newRef.child(newRef.getKey()).setValue(s);
                    final Query queryRef = myRef.orderByChild("emailId").equalTo("ameerhamzaaaa");

                 queryRef.addListenerForSingleValueEvent(new ValueEventListener() {
                     @Override
                     public void onDataChange(DataSnapshot dataSnapshot) {
                         Log.d("Firebase UID ",dataSnapshot.getKey());//it always null 
                         Student s = dataSnapshot.getValue(Student.class);



                     }

                     @Override
                     public void onCancelled(DatabaseError databaseError) {

                     }
                 });

2 个答案:

答案 0 :(得分:2)

您正在创建一个额外的孩子,并且您正在父母中进行搜索。尝试这样的事情:

final FirebaseDatabase database = FirebaseDatabase.getInstance();
myRef = database.getReference();
DatabaseReference newRef = myRef.push();

i++;
Student s = new Student("ameerhamza",i,"hamza");
newRef.setValue(s);

myRef
  .orderByChild("emailId")
  .equalTo("ameerhamzaaaa")
  .addListenerForSingleValueEvent(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 Map<String, Student> data = dataSnapshot.getValue(new GenericTypeIndicator<Map<String, Student>>() {});
                 Map.Entry<String, Student> entry = data.entrySet().iterator().next();
                 String uid = entry.getKey();
                 Student s = entry.getValue();
                 Log.d("Firebase UID ", uid);
             }

             @Override
             public void onCancelled(DatabaseError databaseError) {
                 // handle error
             }
  });

它可能无法为您提供确切的ID,但应该让您更好地了解正在发生的事情。

答案 1 :(得分:0)

经过这么多辛苦工作后,所有的狐狸,最后我得到了我的ANS,以下是我从firebase获取UID的方法,我使用 Firebase Recyler适配器获取数据从firebase和下一个你看到你自己做了什么

    adapter = new FirebaseRecyclerAdapter<Student, myViewHolader>(


            Student.class,R.layout.item_row,myViewHolader.class,myRef
    ) {
        @Override
        protected void populateViewHolder(myViewHolader viewHolder, Student model, int position) {

            Log.e("myFirebase UID",adapter.getRef(position).getKey());

        }
    };

    recyclerView.setAdapter(adapter);