如何将orderByKey()与DataSnapshot一起使用?

时间:2016-04-01 10:05:55

标签: android firebase firebase-realtime-database

我正在填充RecyclerView中的程序列表。现在我需要按添加的时间对程序进行排序。所以我想为每个程序添加一个timestamp对象。

但我读到PushID也是从时间戳生成的。所以我尝试使用orderByKey()。但我无法弄清楚我如何使用userSnapshot的查询?

CODE

public void FetchData() {
        ProgramItemList.clear();
        mRef.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
                        for (DataSnapshot programSnapshot : userSnapshot.getChildren()) {
                            program = programSnapshot.getValue(Program.class);
                            ProgramItem item = new ProgramItem();
                            String pushID = programSnapshot.getKey();
                            item.setID(pushID);
                            item.setName(program.getTitle());
                            ProgramItemList.add(item);
                        }
                    }
                }
                mAdapter.notifyDataSetChanged();
            }
            @Override
            public void onCancelled(FirebaseError firebaseError) {
            }
        });

    }

JSON

{
  "subscriptions" : {
    "han@gmail,com" : {
      "-KDnhRwHjssOejrqyenP" : {
        "category" : "Strength",
        "goal" : "This workout can be done while on the phone!",
        "length" : 1,
        "title" : "Hello Workouts",
        "weeks" : {
          "week1" : [ "High Knees", "Jumping Jacks", "Burpees" ]
        }
      },
      "-KDni3TN4NMyGXePyp92" : {
        "category" : "Strength",
        "goal" : "This workout can be done by a BABUJI",
        "length" : 1,
        "title" : "Indian Workouts",
        "weeks" : {
          "week1" : [ "Diamond Pushups", "Jackknives", "Pushups" ]
        }
      }
    },
    "obama@gmsil,com" : {
      "-KDnfjROKeFAL9wccsxY" : {
        "category" : "Mobility",
        "goal" : "afternoon body weight workout",
        "length" : 1,
        "title" : "Afternoon HiiT",
        "weeks" : {
          "week1" : [ "High Knees", "Squats", "Lunges", "Diamond Push-ups", "Lying Triceps Lifts" ]
        }
      },
      "-KDps90Dn6XtJc6Co00b" : {
        "category" : "Strength",
        "goal" : "goal",
        "length" : 1,
        "title" : "title",
        "weeks" : {
          "week1" : [ "Diamond Pushups", "Spiderman Push-up", "Wide Arm Push-ups", "Burpee Pushups" ]
        }
      }
    }
  }
  }

0 个答案:

没有答案