Jacob Celestine Jacob Celestine - 28 days ago 13
Java Question

How can I get node name while I am using orderByChild in Firebase?

Here's my Code:

@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);
}


As you guys probably guessed, this code is for displaying a users name. But what I actually want to do is, based on the same query result, I want to display their User ids,not name. I tried using a class like this to get the user 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;
}}


But this returned the current users id 3 times, since I've 3 users in my DB, not individual user ids.
Here's my DB:

Each user have this many values

Another method I tried was using getKey() like this:

@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);
}


But this returned the node 'Users' 3 times, not the User ids.

Can anyone help me with this? I want to display their User ids.That is, these:

OMbACIWg3fOurXy8SEqQD8TAQl12, kZllc0GL1Vfbitt73WHWwJuXvXC2, lxVykDo9GQRcFmb0u1QZ0WCc9lg2
(based on the picture)

Any help is highly appreciated. Thank you.

Answer

You are calling the getKey() method at the wrong DataSnapshot. This is what you want

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

EDIT: To set the userId to the User class

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