我需要遍历Firebase架构以获取每次锻炼的数据.在RecyclerVIEw中显示它.
现在,我无法使用侦听器&在Firebase中遍历该模式.通过称为程序的POJO类获取它.
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 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]}, Title=Indian Workouts, category=Strength, length=1, goal=This workout can be done by a BABUJI}, -KDnhRwHJssOejrqyenP={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]}, Title=Hello Workouts, category=Strength, length=1, goal=This workout can be done while on the phone!}}
03-28 08:52:53.967 23584-23584/com.androID.sam I/System.out: {-KDnfjROKeFAL9wccsxY={weeks={week1=[High Knees, Squats, Lunges, Diamond Push-ups, Lying Triceps lifts]}, Title=Afternoon HiiT, category=mobility, length=1, goal=afternoon body weight workout}, -KDps90Dn6XtJc6Co00b={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]}, Title=Title, category=Strength, length=1, goal=goal}}
Program.java
public class Program { private String Title; private String goal; private String category; private int length; HashMap<String, ArrayList<String>> weeks; /** * required public constructor */ public Program() { } public Program(String Title, String goal, String category, int length, HashMap<String, ArrayList<String>> 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<String>> 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) { }});
总结 以上是内存溢出为你收集整理的java-如何遍历Android中的Firebase结构?全部内容,希望文章能够帮你解决java-如何遍历Android中的Firebase结构?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)