java - Firebase retrieving data to customlistview [Android] -
my error:
e/uncaughtexception: com.google.firebase.database.databaseexception: can't convert object of type java.lang.string type com.stitch.fractions.profile.userinfo.userinformation @ com.google.android.gms.internal.zzamy.zze(unknown source) @ com.google.android.gms.internal.zzamy.zzb(unknown source) @ com.google.android.gms.internal.zzamy.zza(unknown source) @ com.google.firebase.database.datasnapshot.getvalue(unknown source) @ com.stitch.fractions.scoreboard.activity.activity.scoreboardactivity$1.ondatachange(scoreboardactivity.java:49) @ com.google.firebase.database.query$1.ondatachange(unknown source) @ com.google.android.gms.internal.zzajp.zza(unknown source) @ com.google.android.gms.internal.zzakp.zzcxi(unknown source) @ com.google.android.gms.internal.zzaks$1.run(unknown source) @ android.os.handler.handlecallback(handler.java:743) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:150) @ android.app.activitythread.main(activitythread.java:5546) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:794) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:684) i/fa: tag manager not found , not used i/process: sending signal. pid: 22183 sig: 9 disconnected target vm, address: 'localhost:8604', transport: 'socket'
my activity class:
public class scoreboardactivity extends appcompatactivity { private firebaseauth mauth; private databasereference dbref; private arraylist<userinformation> marrayuserinfo = new arraylist<>(); private string userid; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_scoreboard); mauth = firebaseauth.getinstance(); userid = mauth.getcurrentuser().getuid(); dbref = firebasedatabase.getinstance().getreference(); displaydata(); } public void displaydata() { dbref.addlistenerforsinglevalueevent(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot users : datasnapshot.getchildren()) { (datasnapshot user : users.getchildren()) { if (user.getkey().equals(userid)) { (datasnapshot sitem : user.getchildren()) { if (!(sitem.getkey().equals("age"))) { userinformation scoreactivity = sitem.getvalue(userinformation.class); marrayuserinfo.add(scoreactivity); scoreadapter sadapter = new scoreadapter(getapplicationcontext(), marrayuserinfo); listview listscore = (listview)findviewbyid(r.id.lvscore); listscore.setadapter(sadapter); } } } } } } @override public void oncancelled(databaseerror databaseerror) { toast.maketext(scoreboardactivity.this, "error kid", toast.length_short).show(); } }); } }
here model class:
public class userinformation { public string name; public string age; public string score; public userinformation() { } public userinformation(string name, string age, string score) { this.name = name; this.age = age; this.score = score; } public string getname() { return name; } public string getage() { return age; } public string getscore(){ return score; } } }
here how wrote database:
public class profileactivity extends appcompatactivity { private firebaseauth firebaseauth; private textview museremail, muserscore; private button mbtnlogout, mbtnsave, mbtnscore; private edittext medtprofilename, medtprofileage; private databasereference dbrefer; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_profile); initcomponent(); getcurrentuser(); checkcurrentuser(); firebasedatabase(); mbtnlogout.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { firebaseauth.signout(); finish(); intent = new intent(profileactivity.this, homeactivity.class); startactivity(back); toast.maketext(profileactivity.this, "good bye", toast.length_short).show(); } }); mbtnsave.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { saveuserinfor(); } }); mbtnscore.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent game = new intent(profileactivity.this, scoreboardactivity.class); startactivity(game); } }); } public void initcomponent() { mbtnlogout = (button) findviewbyid(r.id.btnout); museremail = (textview) findviewbyid(r.id.useremail); firebaseauth = firebaseauth.getinstance(); medtprofilename = (edittext) findviewbyid(r.id.edtprofilename); medtprofileage = (edittext) findviewbyid(r.id.edtprofileage); mbtnsave = (button) findviewbyid(r.id.btnsave); muserscore = (textview) findviewbyid(r.id.userscore); mbtnscore = (button) findviewbyid(r.id.btnscore); } public void checkcurrentuser() { firebaseauth = firebaseauth.getinstance(); if (firebaseauth.getcurrentuser() == null) { finish(); intent home = new intent(profileactivity.this, homeactivity.class); startactivity(home); } } public void getcurrentuser() { firebaseuser user = firebaseauth.getcurrentuser(); museremail.settext("current user: " + user.getemail()); } public void firebasedatabase() { dbrefer = firebasedatabase.getinstance().getreference(); medtprofilename.gettext().tostring().trim(); medtprofileage.gettext().tostring().trim(); } public void saveuserinfor() { string name = medtprofilename.gettext().tostring().trim(); string age = medtprofileage.gettext().tostring().trim(); sharedpreferences prefsname = getsharedpreferences("name", mode_private); sharedpreferences.editor editorname = prefsname.edit(); editorname.putstring("sentname", name); editorname.commit(); sharedpreferences prefsage = getsharedpreferences("age", mode_private); sharedpreferences.editor editorage = prefsage.edit(); editorage.putstring("sentage", age); editorage.commit(); sharedpreferences prefs = getsharedpreferences("data",mode_private); string data = prefs.getstring("time","hi"); if (textutils.isempty(name)) { medtprofilename.seterror("empty field !!!"); return; } if (textutils.isempty(age)) { medtprofileage.seterror("empty field !!!"); return; } userinformation userinfo = new userinformation(name, age, data); firebaseuser user = firebaseauth.getcurrentuser(); dbrefer.child("user").child(user.getuid()).setvalue(userinfo); toast.maketext(this, "data saved", toast.length_short).show(); } }
my adapter class:
public class scoreadapter extends baseadapter { private context mcontext; private layoutinflater mscoreinflater; private arraylist<userinformation> muserinfo; public scoreadapter(context mcontext, arraylist<userinformation> userinfo) { this.mcontext = mcontext; this.mscoreinflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service); this.muserinfo = userinfo; } @override public int getcount() { return muserinfo.size(); } @override public object getitem(int position) { return muserinfo.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { classscoreholder holder; if (convertview == null) { convertview = mscoreinflater.inflate(r.layout.scoreboard_row, parent, false); holder = new classscoreholder(); holder.mtvscorename = (textview) convertview.findviewbyid(r.id.tvscorename); holder.mtvscorepoint = (textview) convertview.findviewbyid(r.id.tvscorepoint); convertview.settag(holder); } else { holder = (classscoreholder) convertview.gettag(); } textview tvscorename = holder.mtvscorename; textview tvscorepoint = holder.mtvscorepoint; userinformation getuserinfo = (userinformation) getitem(position); tvscorename.settext("user name: " + getuserinfo.getname()); tvscorepoint.settext("user score: " + getuserinfo.getscore()); return convertview; } private static class classscoreholder { public textview mtvscorename; public textview mtvscorepoint; }
current database has name, age , score. i'm trying name , age firebase database customlistview.
not 100% sure, go 1 level far in hierarchy. if right, simplify code. try :
public void displaydata() { dbref.addlistenerforsinglevalueevent(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { (datasnapshot users : datasnapshot.getchildren()) { (datasnapshot user : users.getchildren()) { if (user.getkey().equals(userid)) { userinformation scoreactivity = user.getvalue(userinformation.class); marrayuserinfo.add(scoreactivity); } } } scoreadapter sadapter = new scoreadapter(getapplicationcontext(), marrayuserinfo); listview listscore = (listview)findviewbyid(r.id.lvscore); listscore.setadapter(sadapter); } @override public void oncancelled(databaseerror databaseerror) { toast.maketext(scoreboardactivity.this, "error kid", toast.length_short).show(); } }); }
Comments
Post a Comment