1

I'm making an application(pedometer) in android-studio where I want to list data, that I take from the realtime-databse in firebase, into a Listview within my app. Due to linking dates with steps on each day I'm left with a setup that gives "Date:steps". What do I need to do in order to accumulate all steps from each day into a subtotal that only lists the total steps without the date?

This is for a leaderboard page I'm making where I want to organize all users from highest steps(walked on all dates, starts counting from date of registration) to the lowest. I've tried to make a for loop where I call for the user and steps walked, but the steps are still linked to the date.

I've tried looking at other similar questions, example: How do I efficiently iterate over each entry in a Java Map?

But I still can't make sense into it.

The code in question is:

myRef.addChildEventListener(new ChildEventListener() {
@Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, String s) {
        Map<String, Map> map = (Map<String, Map>) dataSnapshot.getValue();

        int i  = 0;

        for (Map.Entry<String, Map> pair : map.entrySet()) {
            Map steps = map.get("steps");

            arrayList.add(" " + map.get("username") + " " + steps);
            arrayAdapter = new ArrayAdapter<>(Leaderboard.this , android.R.layout.simple_list_item_1 , arrayList);
      }

        listView.setAdapter(arrayAdapter);
     }

I want to get the output:

Name 200

with number "200" being the total amount of steps walked over all dates.
The output I am getting at the moment is:

Name {Date=37,Date=40,Date=12,Date=67,Date=44}
Name {Date=37,Date=40,Date=12,Date=67,Date=44}
Name {Date=37,Date=40,Date=12,Date=67,Date=44}

What would I need to do in order to remove the dates, and list only the values(steps) as a subtotal of all steps walked over said listed dates(per user)?

vincrichaud
  • 2,218
  • 17
  • 34
Molinho
  • 31
  • 1
  • 6
  • 1
    Your `map` is a `Map`, so what's the structure of the inner `Map`? ``? ``? Etc... – BeUndead May 27 '19 at 09:01
  • Iterate through your `map` get the values from each entry, stream it and sum it up as they're just ints. – Yoda May 27 '19 at 09:06
  • apologies for the late reply @user2478398 Already figured out how to fix it, the inner map was a `` – Molinho May 27 '19 at 13:35

0 Answers0