如何只读取一个密钥以从Firebase数据库中获取价值?

时间:2017-11-07 16:10:36

标签: android firebase-realtime-database

我想在5A4WDqhepiOwy4fDKM9OzocHNdX2只获得价值(地址和电话)!

这是我在Firebase数据库中的数据库:

  "users" : {
    "5A4WDqhepiOwy4fDKM9OzocHNdX2" : {
      "address" : "123456",
      "displayname" : "han",
      "email" : "phamvanhan68@gmail.com",
      "id" : "5A4WDqhepiOwy4fDKM9OzocHNdX2",
      "phone" : "1234567890"
    },
    "iFFzXSnwMDTWXbxKIFmnVuw18Wf1" : {
      "address" : "han",
      "displayname" : "han",
      "email" : "111111@gmail.com",
      "id" : "iFFzXSnwMDTWXbxKIFmnVuw18Wf1",
      "phone" : "0123456789"
    }
  }

这是我的代码!

mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
FirebaseUser user = mAuth.getCurrentUser();
idUser = user.getUid();
myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        DataSnapshot dataSnapshotTyperService =dataSnapshot.child("users");
        getInfor(dataSnapshotTyperService);
        Log.d("kiemtra12345", idUser+"");

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

void getInfor (DataSnapshot dataSnapshot){
        for(DataSnapshot ds : dataSnapshot.getChildren()){
            User uInfo = new User();
            uInfo.setDisplayname(ds.child(idUser).getValue(User.class).getDisplayname());
            uInfo.setPhone(ds.child(idUser).getValue(User.class).getPhone());
            uInfo.setAddress(ds.child(idUser).getValue(User.class).getAddress());

            Log.d("kiemtra123451", "showData: name: " + uInfo.getDisplayname());
            Log.d("kiemtra123452", "showData: email: " + uInfo.getEmail());
            Log.d("kiemtra123453", "showData: phone_num: " + uInfo.getPhone());


            ArrayList<String> array  = new ArrayList<>();
            array.add(uInfo.getDisplayname());
            array.add( uInfo.getEmail());
            array.add(uInfo.getPhone());
            ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
            lvtest.setAdapter(adapter);

            Log.d("kiemtraaaaaaaaaaaaaaa",lvtest+"");
        }
    }

1 个答案:

答案 0 :(得分:0)

FirebaseAuth mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
DatabaseReference myRef = mFirebaseDatabase.getReference().child("user/Harry");

myRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User myUser = dataSnapshot.getValue(User.class);
        Log.d("FB", myUser.getAddress());
        Log.d("FB", myUser.getPhone());
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        Log.e(TAG, "onCancelled", databaseError.toException());
    }
});