如何在Android中遍历Firebase结构?

我需要遍历Firebase架构以获取每个workouts数据并将其显示在RecyclerView中。 现在我无法使用侦听器遍历Firebase中的Schema并通过名为Program POJO Class获取它。

来自Firebase的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", "Wall sit", "Pushups", "Sit-ups", "Step ups", "Squats", "Tricep dips on chair", "Plank", "High Knees running in place", "Lunges", "Pushup and rotation", "Side plank (alternate per round)", "Alternating Push-Up Plank", "Chest Expander", "Diamond Push-ups", "Dive Bomber Push-ups", "Butt Kickers", "Lying Triceps Lifts", "One Arm Side Push-up", "Overhead Arm Clap", "Overhead Press", "Power Circles", "Push-up and Rotation", "T Push-ups", "Reverse Plank", "Spiderman Push-up", "T Raise", "Tricep Dips", "Wall Push-ups", "Wide Arm Push-ups", "Burpees" ] } }, "-KDni3TN4NMyGXePyp92" : { "category" : "Strength", "goal" : "This workout can be done by a BABUJI", "length" : 1, "title" : "Indian Workouts", "weeks" : { "week1" : [ "Diamond Pushups", "Jackknives", "Plyo Lunges", "Plyo Squats", "Single leg plank (alternate per round)", "Plyo Lunges", "Pushup and rotation", "Weighted side plank (alternate per round)", "Alternating Push-Up Plank", "Chest Expander", "Diamond Push-ups", "Dive Bomber Push-ups", "One Arm Side Push-up", "Overhead Press", "Push-up and Rotation", "T Push-ups", "Spiderman Push-up", "Wide Arm Push-ups", "Burpee 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", "Jackknives", "Plyo Lunges", "Plyo Squats", "Single leg plank (alternate per round)", "Plyo Lunges", "Pushup and rotation", "Weighted side plank (alternate per round)", "Alternating Push-Up Plank", "Chest Expander", "Diamond Push-ups", "Dive Bomber Push-ups", "One Arm Side Push-up", "Overhead Press", "Push-up and Rotation", "T Push-ups", "Spiderman Push-up", "Wide Arm Push-ups", "Burpee Pushups" ] } } } } } 

听众代码

  public void FB(){ ref.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot snapshot) { for (DataSnapshot postSnapshot: snapshot.getChildren()) { System.out.println(postSnapshot.getValue()); //Program post = proSnapshot.getValue(Program.class); } } @Override public void onCancelled(FirebaseError firebaseError) { System.out.println("The read failed: " + firebaseError.getMessage()); } }); } 

logcat的

03-28 08:52:53.966 23584-23584 / com.android.sam I / System.out: {-KDni3TN4NMyGXePyp92 = {weeks = {week1 = [Diamond Pushups,Jackknives,Plyo Lunges,Plyo Squats,Single leg plank(alternate)每轮),Plyo Lunges,俯卧撑和旋转,加权侧板(每轮交替),交替俯卧撑,胸部扩张器,钻石俯卧撑,潜水轰炸机俯卧撑,单臂侧俯卧撑,架空压力机,俯卧撑和旋转,T型俯卧撑,蜘蛛侠俯卧撑,宽臂俯卧撑,Burpee俯卧撑]},title =印度锻炼,类别=力量,长度= 1,目标=此锻炼可由BABUJI完成},-KDnhRwHjssOejrqyenP = {周= {周1 = [高膝盖,跳跃杰克,坐姿,俯卧撑,仰卧起坐,boost,下蹲,三头肌蹲在椅子上,普朗克,高膝跑到位,弓步,俯卧撑和旋转,侧板(每轮交替),交替俯卧钢板,胸部扩张器,钻石俯卧撑,潜水轰炸机俯卧撑,对接踢球器,躺式三头肌升降机,单臂侧俯卧撑,架空臂拍,架空压力机,力量圈,脓 h-up和Rotation,T俯卧撑,反向木板,蜘蛛侠俯卧撑,T提升,三头肌下垂,俯卧撑,宽臂俯卧撑,Burpees]},title = Hello锻炼,类别=力量,长度= 1,goal =这项训练可以在手机上完成!}}

03-28 08:52:53.967 23584-23584 / com.android.sam I / System.out: {-KDnfjROKeFAL9wccsxY = {week = {week1 = [高膝,蹲下,弓步,钻石俯卧撑,趴三头肌举升] },title =下午HiiT,类别=流动性,长度= 1,目标=下午体重锻炼}, – KDps90Dn6XtJc6Co00b = {周= {周1 = [钻石俯卧撑,Jackknives,Plyo Lunges,Plyo深蹲,单腿板(替代)圆形),Plyo Lunges,俯卧撑和旋转,加权侧板(每轮交替),交替俯卧撑,胸部扩张器,钻石俯卧撑,潜水轰炸机俯卧撑,单臂侧俯卧撑,架空压力机,推俯卧撑和旋转,T型俯卧撑,蜘蛛侠俯卧撑,宽臂俯卧撑,Burpee俯卧撑]},标题=标题,类别=力量,长度= 1,目标=目标}}

Program.java

 public class Program { private String title; private String goal; private String category; private int length; HashMap<String, ArrayList> weeks; /** * Required public constructor */ public Program() { } public Program(String title, String goal, String category, int length, HashMap<String, ArrayList> weeks) { this.title = title; this.goal = goal; this.category = category; this.length = length; this.weeks = weeks; } public String getTitle() { return title; } public String getGoal() { return goal; } public String getCategory() { return category; } public int getLength() { return length; } public HashMap<String, ArrayList> getweeks() { return weeks; } } 

正如@ReazMurshed所说,你几乎就在那里。 但是你没有在树中迭代一个级别。 似乎ref指向JSON中的subscriptions节点。 在那之下,你有用户,然后你有推动ID。 所以你需要做一个双循环:

 ref.addListenerForSingleValueEvent(new ValueEventListener) { public void onDataChange(DataSnapshot snapshot) { if (dataSnapshot.exists()) { for (DataSnapshot userSnapshot : snapshot.getChildren()) { for (DataSnapshot programSnapshot : userSnapshot.getChildren()) { Program program = programSnapshot.getValue(Program.class); } } } public void onCancelled(FirebaseError firebaseError) { } }); 

你快到了。 您只需要添加addListenerForSingleValueEvent侦听器以及迭代浏览与引用节点关联的数据的引用。 这是一个如何完成它的例子。

在您的情况下,我们只需声明一个名为ProgramList的类

 public class ProgramList { public List mProgramList; } 

现在获取对电子邮件地址节点的引用并为其添加一个侦听器。

 ref.addListenerForSingleValueEvent(new ValueEventListener) { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.exists()) { for (DataSnapshot programSnapshot : dataSnapshot.getChildren()) { final ProgramList mProgramList = programSnapshot.getValue(ProgramList.class); // Now iterate through the list here. } } } @Override public void onCancelled(FirebaseError firebaseError) { } }); 

您可以检查firebase文档,以便再次在android中检索数据。 这是与此处相同的数据库结构的示例 。