java - FirebaseRecyclerAdapter doesn't work for me -
i need parent key it's child keys represent list of latitude , longitude in recyclerview. guess need latlngsmodel item
in populateviewholder
inside firebaserecycleradapter
no matter don't right.
i loops trough parent keys child keys many parent keys has, , adds list...
the json structure:
{ "users" : { "0057242b-81e2-4f97-bca7-b671212614ba" : { "email" : "kalle@hotmail.se", "waypoints" : { "-kh9uaph5nmljexaua5g" : { "-kh9uaph5nmljexas2s" : { "latitude" : 111, "longitude" : 111.1 } }, "-khb1vjqudo90vxj9xch" : { "-khb1vjqudo90vxj9xci" : { "latitude" : 222.1, "longitude" : 222.11 }, "-khb1zykbwgxm9spnie9" : { "latitude" : 222.2, "longitude" : 222.22 } } } },
updated code
user
@jsonignoreproperties(ignoreunknown=true) public class user{ @jsonproperty("email") string email; mywaypoint waypoints; public user(){} public user(string email){ this.email = email; } public string getemail(){return this.email; } public void setemail(string _email){this.email = _email;} public mywaypoint getwaypoints(){return waypoints;} public void setwaypoints(mywaypoint points){ this.waypoints = points;} }
latitudes , longitudes pojo.
@jsonignoreproperties(ignoreunknown = true) public class mywaypoint { private double latitude; private double longitude; public mywaypoint() { } public mywaypoint(double latitude, double longitude) { this.latitude = latitude; this.longitude = longitude; } public double getlatitude() { return latitude; } public double getlongitude() { return longitude; } }
my activity
public class maplistactivityrealback2 extends appcompatactivity { private string log_tag = "log_tag"; private firebase mref; private firebase userref; private string muserid; firebaserecycleradapter<user, latlngviewholderback2> madapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_list); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); mref = new firebase(constants.firebase_url); if (mref.getauth() == null) { loadloginview(); } try { muserid = mref.getauth().getuid(); } catch (exception e) { loadloginview(); } final recyclerview mrecyclerview = (recyclerview) findviewbyid(r.id.card_recycler_view); mrecyclerview.sethasfixedsize(false); linearlayoutmanager manager = new linearlayoutmanager(this); manager.setreverselayout(false); mrecyclerview.setlayoutmanager(manager); // https://todoapprj.firebaseio.com/users/1a96a633-7e67-41b8-9aa7-c70d4b7eb59c final string userurl = constants.firebase_url + "/users/" + muserid; userref = new firebase(userurl); madapter = new firebaserecycleradapter<user, latlngviewholderback2>(user.class, r.layout.list_item, latlngviewholderback2.class, userref) { @override protected void populateviewholder(final latlngviewholderback2 latlngviewholder, user item, final int i) { userref.addvalueeventlistener(new valueeventlistener() { list<mywaypoint> userwaypointslist = new arraylist<mywaypoint>(); @override public void ondatachange(datasnapshot waypointsdatasnapshot) { if(waypointsdatasnapshot.getchildrencount() > 0){ (datasnapshot waypointssnapshotchild : waypointsdatasnapshot.getchildren()){ log.i("firebasetester", "for-loop :: waypointssnapshotchild.getvalue() : "+waypointssnapshotchild.getvalue()); if(waypointssnapshotchild.getchildrencount()>0){ (datasnapshot waypointschild : waypointssnapshotchild.getchildren()){ //this lat , lon double latitude = double.parsedouble(waypointschild.child("latitude").getvalue().tostring()); double longitude = double.parsedouble(waypointschild.child("longitude").getvalue().tostring()); userwaypointslist.add(new mywaypoint( latitude, longitude)); log.i("firebasetester","latitude = "+latitude+" , longitude = "+longitude); } } } } //here can assign points user log.i("firebasetester","there "+userwaypointslist.size()+ " points user"); } @override public void oncancelled(firebaseerror firebaseerror) { log.e("firebasetester", "oncancelled - waypointref error " + firebaseerror.getmessage()); } }); } }; mrecyclerview.setadapter(madapter); } private void loadloginview() { intent intent = new intent(this, loginactivity.class); intent.addflags(intent.flag_activity_new_task); intent.addflags(intent.flag_activity_clear_task); startactivity(intent); } }
fatal exception: main process: com.example.rasmusjosefsson.rjcar, pid: 19134 com.firebase.client.firebaseexception: failed bounce type @ com.firebase.client.datasnapshot.getvalue(datasnapshot.java:185) @ com.firebase.ui.firebaserecycleradapter.parsesnapshot(firebaserecycleradapter.java:161) @ com.firebase.ui.firebaserecycleradapter.getitem(firebaserecycleradapter.java:150) @ com.firebase.ui.firebaserecycleradapter.onbindviewholder(firebaserecycleradapter.java:190) @ android.support.v7.widget.recyclerview$adapter.onbindviewholder(recyclerview.java:5471) @ android.support.v7.widget.recyclerview$adapter.bindviewholder(recyclerview.java:5504) @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:4741) @ android.support.v7.widget.recyclerview$recycler.getviewforposition(recyclerview.java:4617) @ android.support.v7.widget.linearlayoutmanager$layoutstate.next(linearlayoutmanager.java:1994) @ android.support.v7.widget.linearlayoutmanager.layoutchunk(linearlayoutmanager.java:1390) @ android.support.v7.widget.linearlayoutmanager.fill(linearlayoutmanager.java:1353) @ android.support.v7.widget.linearlayoutmanager.onlayoutchildren(linearlayoutmanager.java:574) @ android.support.v7.widget.recyclerview.dispatchlayoutstep2(recyclerview.java:3028) @ android.support.v7.widget.recyclerview.dispatchlayout(recyclerview.java:2906) @ android.support.v7.widget.recyclerview.onlayout(recyclerview.java:3283) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.support.design.widget.headerscrollingviewbehavior.layoutchild(headerscrollingviewbehavior.java:122) @ android.support.design.widget.viewoffsetbehavior.onlayoutchild(viewoffsetbehavior.java:42) @ android.support.design.widget.appbarlayout$scrollingviewbehavior.onlayoutchild(appbarlayout.java:1170) @ android.support.design.widget.coordinatorlayout.onlayout(coordinatorlayout.java:814) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.widget.framelayout.layoutchildren(framelayout.java:336) @ android.widget.framelayout.onlayout(framelayout.java:273) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.widget.linearlayout.setchildframe(linearlayout.java:1743) @ android.widget.linearlayout.layoutvertical(linearlayout.java:1586) @ android.widget.linearlayout.onlayout(linearlayout.java:1495) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.widget.framelayout.layoutchildren(framelayout.java:336) @ android.widget.framelayout.onlayout(framelayout.java:273) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.widget.linearlayout.setchildframe(linearlayout.java:1743) @ android.widget.linearlayout.layoutvertical(linearlayout.java:1586) @ android.widget.linearlayout.onlayout(linearlayout.java:1495) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.widget.framelayout.layoutchildren(framelayout.java:336) @ android.widget.framelayout.onlayout(framelayout.java:273) @ com.android.internal.policy.phonewindow$decorview.onlayout(phonewindow.java:2678) @ android.view.view.layout(view.java:16630) @ android.view.viewgroup.layout(viewgroup.java:5437) @ android.view.viewrootimpl.performlayout(viewrootimpl.java:2171) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:1931) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1107) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6013) @ android.view.choreographer$callbackrecord.run(choreographer.java:858) @ android.view.choreographer.docallbacks(choreographer.java:670) @ android.view.choreographer.doframe(choreographer.java:606) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:844) @ android.os.h
updated answer change firebase url final string userurl = constants.firebase_url + "/users"
(get rid of rest of url). have done in new code iterate through data , create mywaypoint
instances each lat-lon pairs. have indicated in code can start working mywaypoint
whatever desire:
userref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { log.i("firebasetester", "ondatachange()"); if (datasnapshot.getchildrencount() > 0) { log.i("firebasetester", "there "+datasnapshot.getchildrencount()+" users"); (datasnapshot usersnapshot : datasnapshot.getchildren()) { string email = null; if(usersnapshot.child("email") != null) { //this how value of email email = usersnapshot.child("email").getvalue().tostring(); log.i("firebasetester", "user email:"+email+" plain"); } if(usersnapshot.child("waypoints") !=null){ if(usersnapshot.child("waypoints").getchildrencount() > 0){ firebase waypointref = usersnapshot.child("waypoints").getref(); if(waypointref != null){ waypointref.addvalueeventlistener(new valueeventlistener() { //this keeps per-user list of points list<mywaypoint> userwaypointslist = new arraylist<mywaypoint>(); @override public void ondatachange(datasnapshot waypointsdatasnapshot) { if(waypointsdatasnapshot.getchildrencount() > 0){ (datasnapshot waypointssnapshotchild : waypointsdatasnapshot.getchildren()){ log.i("firebasetester", "for-loop :: waypointssnapshotchild.getvalue() : "+waypointssnapshotchild.getvalue()); if(waypointssnapshotchild.getchildrencount()>0){ (datasnapshot waypointschild : waypointssnapshotchild.getchildren()){ //this lat , lon double latitude = double.parsedouble(waypointschild.child("latitude").getvalue().tostring()); double longitude = double.parsedouble(waypointschild.child("longitude").getvalue().tostring()); userwaypointslist.add(new mywaypoint( latitude, longitude)); log.i("firebasetester","latitude = "+latitude+" , longitude = "+longitude); } } } } //here can assign points user log.i("firebasetester","there "+userwaypointslist.size()+ " points user"); } @override public void oncancelled(firebaseerror firebaseerror) { log.e("firebasetester", "oncancelled - waypointref error " + firebaseerror.getmessage()); } }); } } else{ log.i("firebasetester", "no waypoints data received"); } } } } else{ //no data? log.i("firebasetester", "no data received"); } } @override public void oncancelled(firebaseerror firebaseerror) { log.e("firebasetester", "oncancelled - error "+firebaseerror.getmessage()); } });
latest update: indicated prefer retrieve user-specific data instead of list of users (like suggested in code above). below code user specific data. url should like: final string userurl = constants.firebase_url + "/users/" + muserid;
userref.myfirebaseref.addvalueeventlistener(new valueeventlistener() { @override public void ondatachange(datasnapshot datasnapshot) { if (datasnapshot.getchildrencount() > 0) { log.i("firebasetester", "there "+datasnapshot.getchildrencount()+" user attributes"); string email = null; if(datasnapshot.child("email") != null) { //this how value of email email = datasnapshot.child("email").getvalue().tostring(); log.i("firebasetester", "user email:"+email+" plain"); } if(datasnapshot.child("waypoints") !=null){ if(datasnapshot.child("waypoints").getchildrencount() > 0){ firebase waypointref = datasnapshot.child("waypoints").getref(); if(waypointref != null){ waypointref.addvalueeventlistener(new valueeventlistener() { //this keeps per-user list of points list<mywaypoint> userwaypointslist = new arraylist<mywaypoint>(); @override public void ondatachange(datasnapshot waypointsdatasnapshot) { if(waypointsdatasnapshot.getchildrencount() > 0){ (datasnapshot waypointssnapshotchild : waypointsdatasnapshot.getchildren()){ log.i("firebasetester", "for-loop :: waypointssnapshotchild.getvalue() : "+waypointssnapshotchild.getvalue()); if(waypointssnapshotchild.getchildrencount()>0){ (datasnapshot waypointschild : waypointssnapshotchild.getchildren()){ //this lat , lon double latitude = double.parsedouble(waypointschild.child("latitude").getvalue().tostring()); double longitude = double.parsedouble(waypointschild.child("longitude").getvalue().tostring()); userwaypointslist.add(new mywaypoint( latitude, longitude)); log.i("firebasetester","latitude = "+latitude+" , longitude = "+longitude); } } } } //here can assign points user log.i("firebasetester","there "+userwaypointslist.size()+ " points user"); } @override public void oncancelled(firebaseerror firebaseerror) { log.e("firebasetester", "oncancelled - waypointref error " + firebaseerror.getmessage()); } }); } } else{ log.i("firebasetester", "no waypoints data received"); } } } else{ //no data? log.i("firebasetester", "no user attributes"); } } @override public void oncancelled(firebaseerror firebaseerror) { log.e("firebasetester", "oncancelled - error "+firebaseerror.getmessage()); } });
i hope makes things easier you. way, useful show code use recyclerview - in case might want @ example here.
Comments
Post a Comment