我想从firebase数据库中读取特定数据.我目前正在做的是这里.
DatabaseReference database = FirebaseDatabase.getInstance().getReference(); DatabaseReference myRef = database.child("profiles/"); myRef.child(phoneNo).addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { user = dataSnapshot.getValue(User.class); } @Override public void onCancelled(DatabaseError databaseError) {} });
写入DB的方法
public void writeToDBProfiles(Object data,String phoneNo) { DatabaseReference database = FirebaseDatabase.getInstance().getReference(); DatabaseReference myRef = database.child("profiles/" + phoneNo); myRef.setValue(data); }
但它返回null …任何帮助将不胜感激.
解决方法
如果您想使用电话号码获取特定数据,那么您可以使用这样的查询
DatabaseReference database = FirebaseDatabase.getInstance().getReference(); DatabaseReference ref = database.child("profiles"); Query phoneQuery = ref.orderByChild(phoneNo).equalTo("+923336091371"); phoneQuery.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){ user = singleSnapshot.getValue(User.class); } } @Override public void onCancelled(DatabaseError databaseError) { Log.e(TAG,"onCancelled",databaseError.toException()); } });
我认为这是最好的方法.