1
votes

I need the parent key with it's child keys to represent a list of latitude and longitude in a RecyclerView. Guess I need to do something with the LatLngsModel item in the populateViewHolder inside the FirebaseRecyclerAdapter but no matter what I do I don't get it right.

I currently loops trough all the parent keys with it child keys as many parent keys it has, and adds it to the list...

The JSON structure:

{
  "users" : {
    "0057242b-81e2-4f97-bca7-b671212614ba" : {
      "email" : "[email protected]",
      "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 and 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){
                            for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()){
                                Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : "+wayPointsSnapshotChild.getValue());
                                if(wayPointsSnapshotChild.getChildrenCount()>0){

                                    for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()){
                                        //this is where we get the Lat and 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 you can assign the points to the user
                        Log.i("FireBaseTester","There are "+userWayPointsList.size()+ " Points for User");
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + 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 to bounce to type at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) at com.firebase.ui.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:161) at com.firebase.ui.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:150) at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:190) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471) at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741) at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617) at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994) at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390) at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353) at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574) at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028) at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906) at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:122) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1170) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:814) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.H

2
Hi, do you have a User POJO perhaps? I have created one as I feel it would be more helpful than having LatLngsModel - I will post my suggested solution shortly.ishmaelMakitla

2 Answers

0
votes

UPDATED ANSWER Change your Firebase URL to final String userUrl = Constants.FIREBASE_URL + "/users" (get rid of the rest of the URL). What I have done in this new code was to iterate through the data and create MyWaypoint instances for each lat-lon pairs. I have indicated in the code where you can start working with MyWaypoint to do whatever you desire:

 userRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.i("FireBaseTester", "onDataChange()");
                if (dataSnapshot.getChildrenCount() > 0) {
                    Log.i("FireBaseTester", "There are "+dataSnapshot.getChildrenCount()+" Users");
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        String email = null;

                        if(userSnapshot.child("email") != null) {
                            //this is how you get the 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){
                                                for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()){
                                                    Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : "+wayPointsSnapshotChild.getValue());
                                                   if(wayPointsSnapshotChild.getChildrenCount()>0){

                                                       for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()){
                                                           //this is where we get the Lat and 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 you can assign the points to the user
                                            Log.i("FireBaseTester","There are "+userWayPointsList.size()+ " Points for User");
                                        }

                                        @Override
                                        public void onCancelled(FirebaseError firebaseError) {
                                            Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + 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 is "+firebaseError.getMessage());
            }
        });

Latest UPDATE: You indicated that you prefer to retrieve user-specific data instead of a list of all users (like I suggested in the code above). Below is the code for user specific data. Your URL should now be 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 are "+dataSnapshot.getChildrenCount()+" User Attributes");
                        String email = null;
                        if(dataSnapshot.child("email") != null) {
                            //this is how you get the 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){
                                                for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()){
                                                    Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : "+wayPointsSnapshotChild.getValue());
                                                    if(wayPointsSnapshotChild.getChildrenCount()>0){

                                                        for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()){
                                                            //this is where we get the Lat and 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 you can assign the points to the user
                                            Log.i("FireBaseTester","There are "+userWayPointsList.size()+ " Points for User");
                                        }

                                        @Override
                                        public void onCancelled(FirebaseError firebaseError) {
                                            Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + 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 is "+firebaseError.getMessage());
            }
        });

I hope this makes things easier for you. By the way, it would be really useful for you to show the code where you use RecyclerView - in that case you might want to look at a good example here.

0
votes

The solution I found to work, was using List<List<String>> that retrieves waypoints from the onDataChange() and puts them into the nested list.

This nested list is later used in the recyclerView where each list from the nested list represent a "View item" in my recyclerView

    private Firebase mRef;
    private Firebase mUserRef;
    private String mUserId;
    List<List<String>> latitudeNlongitude = new ArrayList<>();


    //  FirebaseRecyclerAdapter<MyLatLng123, LatLngViewHolderBack2> mAdapter;
    FirebaseRecyclerAdapter<MyWaypoint, 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/waypoints
        final String userUrl = Constants.FIREBASE_URL + "/users/" + mUserId + "/waypoints";
        mUserRef = new Firebase(userUrl);


        if (mUserRef != null) {
            mUserRef.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) {


                        for (DataSnapshot wayPointsSnapshotChild : wayPointsDataSnapshot.getChildren()) {
                            Log.i("FireBaseTester", "For-Loop :: wayPointsSnapshotChild.getValue() : " + wayPointsSnapshotChild.getValue());
                            if (wayPointsSnapshotChild.getChildrenCount() > 0) {


                                // Temporary list
                                List<String> latLngListTemp = new ArrayList<>();


                                for (DataSnapshot wayPointsChild : wayPointsSnapshotChild.getChildren()) {

                                    //this is where we get the Lat and Lon
                                    double latitude = Double.parseDouble(wayPointsChild.child("latitude").getValue().toString());
                                    double longitude = Double.parseDouble(wayPointsChild.child("longitude").getValue().toString());
                                    Log.i("FireBaseTester", "latitude = " + latitude + " , longitude = " + longitude);

                                    latLngListTemp.add(latitude + ", " + longitude);


                                }

                                // List containing a nested List<List<String>>
                                latitudeNlongitude.add(latLngListTemp);
                            }

                        }

                    }

                    //here you can assign the points to the user
                    Log.i("FireBaseTester", "There are " + userWayPointsList.size() + " Points for User");
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    Log.e("FireBaseTester", "onCancelled - wayPointRef Error is " + firebaseError.getMessage());
                }
            });
        } else {
            Log.i("FireBaseTester", "No WayPoints Data Received");
        }


        mAdapter = new FirebaseRecyclerAdapter<MyWaypoint, LatLngViewHolderBack2>(MyWaypoint.class, R.layout.list_item, LatLngViewHolderBack2.class, mUserRef) {
            @Override
            protected void populateViewHolder(final LatLngViewHolderBack2 latLngViewHolder, MyWaypoint item, final int i) {




                // Retrieves a List from the position (i) of the nested list at onDataChange --- > List<List<String>> latitudeNlongitude
                List<String> latlngListIterator = latitudeNlongitude.get(i);

                // New Empty list for each iteration
                List<String> latLngListTemporary  = new ArrayList<>();

                // Adding items to a new empty list for use in next method with googles api.
                for (String value : latlngListIterator) {
                    latLngListTemporary.add(value);
                }

                // Getting the start location and the endlocation
                String startLatLng = latLngListTemporary.get(0);
                String endLatLng = latLngListTemporary.get(latLngListTemporary .size() - 1);


                // Retrieves the information from googles API in JSON and turns them into POJOS