如何在Firebase中使用orderByChild时获取节点名称?

时间:2016-11-10 06:35:14

标签: java android firebase firebase-realtime-database nosql

这是我的代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    Query queryRef  = mdatabase.child("Users").orderByChild("age");
    queryRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {
            for (DataSnapshot chatSnapshot: snapshot.getChildren()) {
                User users = chatSnapshot.getValue(User.class);
                String name = users.getName();
                System.out.println(name);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });        // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_home, container, false);
}

正如大家们猜测的那样,此代码用于显示用户名。但我真正想要做的是,基于相同的查询结果,我想显示他们的用户ID,而不是名称。我尝试使用这样的类来获取用户ID:

public class User{
private String gender;
private Long age;
private String email;
private Long height;
private Long weight;
private String name;
private String type;
private String userId;

public User()
{

}
public User(String gender,Long age,String email,Long height,Long weight,String name,String type,String userId)
{
    this.gender=gender;
    this.age=age;
    this.email=email;
    this.height=height;
    this.weight=weight;
    this.name=name;
    this.type=type;
    this.userId = FirebaseAuth.getInstance().getCurrentUser().getUid();
}

public String getId()
{
    return userId;
}

public String getGender()
{
    return gender;
}

public Long getAge()
{
    return age;
}

public String getEmail()
{
    return email;
}

public Long getHeight()
{
    return height;
}

public Long getWeight()
{
    return weight;
}

public String getName()
{
    return name;
}

public String getType()
{
    return type;
}}

但这会将当前用户ID返回3次,因为我的数据库中有3个用户,而不是单个用户ID。 这是我的DB:

每个用户都有这么多的值

Each user have this many values

我尝试的另一种方法是使用像这样的getKey():

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

Query queryRef  = mdatabase.child("Users").orderByChild("age");
queryRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot chatSnapshot: snapshot.getChildren()) {
            String name = snapshot.getKey();
            System.out.println(name);
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});        // Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}

但这会使节点'用户'返回3次,而不是用户ID。

任何人都可以帮我吗?我想显示他们的用户ID。就是这些:

OMbACIWg3fOurXy8SEqQD8TAQl12,kZllc0GL1Vfbitt73WHWwJuXvXC2,lxVykDo9GQRcFmb0u1QZ0WCc9lg2 (根据图片)

非常感谢任何帮助。谢谢。

1 个答案:

答案 0 :(得分:3)

您正在错误getKey()调用DataSnapshot方法。 这就是你想要的

@Override
public void onDataChange(DataSnapshot snapshot) {
    for (DataSnapshot chatSnapshot: snapshot.getChildren()) {
        String key = chatSnapshot.getKey();
        System.out.println(key);
    }
}

编辑:将userId设置为User

for (DataSnapshot chatSnapshot: snapshot.getChildren()) {
    User user = chatSnapshot.getValue(User.class);
    user.setUserId(chatSnapshot.getKey());
}