0

I have a bunch of nested data stored in FirebaseRealTimeDatabase like this:

Database structure

I am able to retrieve the dataset, that is the profile and their respective scores.

ArrayList<String> profileNameList = new ArrayList();
ArrayList<String> profileStatusList = new ArrayList();
ArrayList<int> scoreList = new ArrayList();

databaseReference.child("players")
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Iterable<DataSnapshot> children = dataSnapshot.getChildren();

                    for (DataSnapshot child : children) {
                        Profile profile = child.child("profile").getValue(Profile.class);
                        Scores scores= child.child("scores").getValue(Scores.class);

                        profileNameList.add(profile.getName());
                        profileStatusList.add(profile.getStatus());
                        scoreList.add(scores.getScore());
     }
  }

I am then using using Collections.sort(scoreList) to sort the scores from lowest to highest. My challenge however is, how can I associate the sorted scores(from the lowest to the highest) again with the player profile, e.g.

  Gavin - 15
  Rico - 40

Any help is appreciated.

Boron
  • 99
  • 10
  • 34

1 Answers1

0

To solve this, please use the following lines of code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference playersRef = rootRef.child("players");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("profile").child("name").getValue(String.class);
            long score = ds.child("score").child("baseball").getValue(Long.class);
            Log.d(TAG, name + " - " + score);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
    }
};
playersRef.addListenerForSingleValueEvent(valueEventListener);

The result in your logcat will be:

Rico Hernandes - 40
Gavin Muro - 15
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • the solution you have provided does as the code I posted above. My intention was after getting the data, to sort such that I can show it in the recyclerview from the lowest to highest or vice-versa. – Boron Dec 14 '18 at 17:14
  • In this case, you should use a timestamp for all your objects so you can order according to it, like as in my answer from this **[post](https://stackoverflow.com/questions/43584244/how-to-save-the-current-date-time-when-i-add-new-value-to-firebase-realtime-data)**, right? – Alex Mamo Dec 14 '18 at 19:48
  • Thanks Alex but I found a solution. See, I needed to sort the data in ascending order. So what I did is create a `PlayerObject` and used the `Comparable` interface to assist in sorting the data. – Boron Dec 15 '18 at 11:19
  • Good, so do you think that my answer/comments helped you? – Alex Mamo Dec 16 '18 at 09:21