java - How can I get node name while I am using orderByChild in Firebase? -
here's 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) { (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 layout fragment return inflater.inflate(r.layout.fragment_home, container, false); }
as guys guessed, code displaying users name. want is, based on same query result, want display user ids,not name. tried using class 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 returned current users id 3 times, since i've 3 users in db, not individual user ids. here's db:
each user have many values
another method tried using getkey() 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) { (datasnapshot chatsnapshot: snapshot.getchildren()) { string name = snapshot.getkey(); system.out.println(name); } } @override public void oncancelled(databaseerror databaseerror) { } }); // inflate layout fragment return inflater.inflate(r.layout.fragment_home, container, false); }
but returned node 'users' 3 times, not user ids.
can me this? want display user ids.that is, these:
ombaciwg3fourxy8seqqd8taql12, kzllc0gl1vfbitt73whwwjuxvxc2, lxvykdo9gqrcfmb0u1qz0wcc9lg2 (based on picture)
any highly appreciated. thank you.
you calling getkey()
method @ wrong datasnapshot
. want
@override public void ondatachange(datasnapshot snapshot) { (datasnapshot chatsnapshot: snapshot.getchildren()) { string key = chatsnapshot.getkey(); system.out.println(key); } }
edit: set userid user
class
for (datasnapshot chatsnapshot: snapshot.getchildren()) { user user = chatsnapshot.getvalue(user.class); user.setuserid(chatsnapshot.getkey()); }
Comments
Post a Comment