如何在Android中遍历Firebase结构? [英] How to Traverse a Firebase Structure in Android?

查看:217
本文介绍了如何在Android中遍历Firebase结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要遍历Firebase模式才能获取每个锻炼数据&将其显示在RecyclerView中。
现在,我无法使用侦听器&脚本遍历Firebase中的架构。通过POJO类获取它,名为程序

来自Firebase的JSON

  {
subscriptions:{
han @ gmail,com:{
-KDnhRwHjssOejrqyenP :{
category:Strength,
goal:这个练习可以在电话里完成!,
length:1,
标题:你好锻炼,
周:{
week1:[高膝盖,跳跃的杰克,墙坐,俯卧撑,仰卧起坐上升下蹲三头肌蹲在椅子上木板高位膝盖运动午餐上推和旋转侧板(每轮交替), 交替俯卧撑,扩胸器,钻石俯卧撑,潜水轰炸机俯卧撑,对接吊咀,三头肌升降机,单臂俯卧撑,胳膊拍,头顶按,动力圈,俯卧撑和旋转,T俯卧撑,反向板,蜘蛛人俯卧撑,T提高,三头肌蘸脂 ,Wall Push- ,宽臂武装,Burpees]
}
},
-KDni3TN4NMyGXePyp92:{
category:Strength,
目标:这个锻炼可以通过BABUJI来完成,
length:1,
title:Indian Workouts,
weeks:{
week1:[钻石俯卧撑,刀具,Plyo Lunges,Plyo Squats,单脚板(每轮交替),Plyo Lunges,Pushup和旋转交替俯卧撑,扩胸器,钻石俯卧撑,潜水轰炸机俯卧撑,单臂侧俯卧撑,俯卧撑俯卧撑和旋转俯卧撑蜘蛛人俯卧撑宽臂俯卧撑伯比斯俯卧撑



obama @ gmsil,com:{
-KDnfjROKeFAL9wccsxY:{
category:Mobility,
goal:锻炼,
长度:1,
title:下午HiiT,
周:{
week1:[高膝盖,蹲,Lunges,钻石俯卧撑,三头肌升降机]

$ b $ k









$b :1,
title:title,
weeks:{
week1:[Diamond Pushups,Jackknives,Plyo Lunges,Plyo Squats ,单脚板(每轮交替),Plyo Lunges,Pushup and rotation,加重侧板(每轮交替),交替俯卧撑板,扩胸器,钻石俯卧撑,单臂俯卧撑,头顶俯卧撑,俯卧撑俯仰俯卧撑,T俯卧撑俯卧撑,蜘蛛人俯卧撑,宽臂俯卧撑,Burpee俯卧撑]
}
}
}
}
}
pre>

监听器代码

  public void FB(){
ref.addValu eEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot snapshot){
for(DataSnapshot postSnapshot:snapshot.getChildren()){
System.out.println (postSnapshot.getValue());
// Program post = proSnapshot.getValue(Program.class);

$ b @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 ,刀,Plyo Lunges,Plyo蹲,单腿板(每轮交替),Plyo Lunges,Pushup和旋转,加重侧板(每轮交替),交替俯卧撑,扩胸器,钻石俯卧撑,潜水轰炸机俯卧撑,俯卧撑,俯卧撑,俯卧撑,俯卧撑,俯卧撑,俯卧撑,俯卧撑,俯卧撑俯卧撑,俯卧撑俯卧撑]},title = Indian Workouts,category = Strength,长度= 1,目标=这个训练可以通过BABUJI},-KDnhRwHjssOejrqyenP = {weeks = {week1 = [高膝盖,跳顶,壁坐,俯卧撑,仰卧起坐, ,板凳,高压膝盖运行,Lunges,Pushup和旋转,侧板(每轮交替),交替俯卧撑,扩胸器,钻石俯卧撑,潜水轰炸机俯卧撑,仰卧起坐,三头肌俯卧撑,单臂侧推,架空臂拍,头顶压,动力圈,俯卧撑和旋转,T型俯卧撑,倒立板,蜘蛛人俯卧撑,T型俯卧撑,三头肌蘸脂,墙俯卧撑,宽臂俯卧撑,Burpees]},title =你好锻炼,类别=力量,长度= 1,目标=这个锻炼可以在电话里完成!}}

03-28 08:52:53.967 23584-23584 / com.android.sam I / System.out:{-KDnfjROKeFAL9wccsxY = {weeks = {week1 = [High Knee,Squats,Lunges,Diamond Push-三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,三人组,单腿板(每轮交替),Plyo Lunges,Pushup和旋转,加重侧板(每轮交替),交替俯卧撑,扩胸器,钻石俯卧撑,潜水轰炸机俯卧撑,单臂侧推,俯卧撑,俯卧撑,俯卧撑,旋转俯卧撑,俯卧撑,蜘蛛推勃起俯卧撑]},title = title,category = Strength,length = 1,goal = goal}}

Program.java

  public class Program {
private String title;

私人字符串目标;

私有字符串类别;

private int length;

HashMap< String,ArrayList< String>>周;
$ b $ **
*必需的公共构造函数
* /
public Program(){
}

public Program String title,String goal,String category,int length,HashMap< String,ArrayList< String>> weeks){
this.title = title;
this.goal =目标;
this.category = category;
this.length = length;
this.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;


$ / code $ / pre

解决方案

@ReazMurshed说,你几乎在那里。但是你无法遍历树中的一个层次。看起来ref是指向JSON中的订阅节点。在那之下,你有用户,然后你有推式ID。所以你需要做一个双循环:

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

public void onCancelled(FirebaseError firebaseError){


});


I need to traverse the Firebase schema to get data for each workouts & display it in a RecyclerView. Now I can't traverse the Schema in the Firebase using the listener & get it via POJO Class called Program.

JSON From Firebase

{
  "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" ]
        }
      }
    }
  }
  }

Listener CODE

    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;
    }    
}

解决方案

As @ReazMurshed said, you're almost there. But you're failing to iterate over one level in your tree. It seems that ref is pointing to the subscriptions node in your JSON. Under that you have users and then you have the push ids. So you'll need to do a double loop:

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) {

  }
});

这篇关于如何在Android中遍历Firebase结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆