问题描述
我正在尝试通过在Android Studio中使用子项"UID"来获取父级名称"170002001".请帮忙.在此先感谢:)
I am trying to get parent name "170002001" by using child key "UID" in android studio. Please help. Thanks in advance :)
字符串uid = user.getUid();
String uid = user.getUid();
private void getdata(){
DatabaseReference reference;
reference = FirebaseDatabase.getInstance().getReference();
reference.orderByChild("UID").equalTo(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
keys = childSnapshot.getKey();
status.setText(keys);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
除"170002001"外,我能够检索所有父名称. :(
Except "170002001" i am able to retrieve all parent name. :(
推荐答案
将其放入您的onDataChange(DataSnapshot dataSnapshot)
侦听器
String parent = dataSnapshot.getKey();
由于firebase会为此管理一个可能的父键列表,所以执行此操作
Since firebase will manage a list of possible parent keys for that , do this
DatabaseReference reference;
reference = FirebaseDatabase.getInstance.getReference();
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String parent = childSnapshot.getKey();
Log.i(TAG,parent);
}
}
请注意,这里的引用将是"170002001"
键上方的节点,该键是所有这些键的父级
note that reference here will be the node up above your "170002001"
key, which is the parent of all those keys
如果要在每个节点中获取所有UID,请执行以下操作
If you want to get all UID inside each of those nodes do this
创建一个POJO类来保存该用户ID
Create a POJO class to hold that user id
public class UserPojo {
private String UID;
public UserPojo(String UID) {
this.UID = UID;
}
public UserPojo() {
}
public String getUID() {
return UID;
}
public void setUID(String UID) {
this.UID = UID;
}
}
然后像上面一样做
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {
String parent = childSnapshot.getKey();
UserPojo user = childSnapshot.getValue(UserPojo.class);
//get your User ID
String user = user.getUID();
Log.i(TAG,parent);
}
}
这篇关于在Firebase Android中使用子项键获取父名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!