android - Firebase database data retrieving -Java -
here db structure:
code used data reading:
user="hrcj7"; mdatabase = firebasedatabase.getinstance().getreference().child("user"); query phonequery = mdatabase.orderbychild(user); phonequery.addchildeventlistener(new childeventlistener() { @override public void onchildadded(datasnapshot datasnapshot, string prevchildkey) { user dinosaur = datasnapshot.getvalue(user.class); system.out.println(datasnapshot.getkey() + " " + dinosaur.getemail() + " meters tall."); } @override public void onchildchanged(datasnapshot datasnapshot, string s) { } @override public void onchildremoved(datasnapshot datasnapshot) { } @override public void onchildmoved(datasnapshot datasnapshot, string s) { } @override public void oncancelled(databaseerror databaseerror) { log.e("app", "oncancelled", databaseerror.toexception()); } });
this datasnapsshot value after retrieving:
datasnapshot { key = hrcj7, value = { -kh2-joegxcor-ve3ud5={ username=hrcj7, email=rperera723@gmail.com, imageurl=https://firebasestorage.googleapis.com/v0/b/freelancer-33195.appspot.com/o/blog_images%2fcropped933315999.jpg?alt=media&token=7890f05f-87db-4a9d-9534-02da00225470 } } }
this model class:
public class user { public string email; public string imageurl; public string username; public user(string email,string imageurl,string username) { this.email = email; this.imageurl=imageurl; this.username=username; } public string getemail() { return email; } public void setemail(string email) { this.email = email; } public string getimageurl() { return imageurl; } public void setimageurl(string imageurl) { this.imageurl = imageurl; } public string getusername() { return username; } public void setusername(string username) { this.username = username; } public user() { // default constructor required calls datasnapshot.getvalue(user.class) } }
my problem dinosaur.getemail()
null after retrieving value. can issue? in advance.
here found solution .
in line:
user dinosaur = datasnapshot.getvalue(user.class);
should replaced following:
string key = mdatabase.child("user").child(user).push().getkey(); user dinosaur = datasnapshot.child(key).getvalue(user.class);
Comments
Post a Comment