如何在android中遍历firebase结构?

mbskvtky  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(280)

我需要遍历firebase模式来获取每个 workouts &在recyclerview中显示它。现在我无法使用侦听器遍历firebase中的模式&通过名为 Program .
来自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=[菱形俯卧撑,jackknifes,plyo弓箭步,plyo下蹲,单腿支撑(每轮交替),plyo弓箭步,俯卧撑和旋转,加权侧支撑(每轮交替),交替俯卧撑支撑,胸部扩张器,菱形俯卧撑,俯卧撑俯卧撑,单臂侧俯卧撑,俯卧撑,俯卧撑和旋转,t型俯卧撑,蜘蛛人俯卧撑,宽臂俯卧撑,波比俯卧撑]},title=印度式训练,category=力量,length=1,goal=这项训练可以由婴儿完成},-kdnhrwhjssoejrqyenp={weeks={week1=[高膝,跳高,壁坐,俯卧撑,仰卧撑,踏步,下蹲、三头肌俯卧在椅子上、支架式、高膝原地跑、弓箭步、俯卧撑和旋转、侧支架式(每轮交替)、交替俯卧撑、胸部扩张器、菱形俯卧撑、俯卧撑俯卧撑、踢腿、卧式三头肌举举举、单臂侧俯卧撑、俯卧撑、俯卧撑、力量循环、俯卧撑和旋转,t俯卧撑,反向支撑,蜘蛛人俯卧撑,t抬高,三头肌俯卧撑,墙式俯卧撑,宽臂俯卧撑,burpees]},title=你好训练,category=力量,length=1,goal=这项训练可以在打电话时完成!}}
03-28 08:52:53.967 23584-23584/com.android.sam i/system.out:{-kdnfjrokefal9wccsxy={weeks={week1=[高膝、深蹲、弓箭步、菱形俯卧撑、卧位三头肌举]},title=午后hiit,category=机动性,length=1,goal=午后体重训练},-kdps90dn6xtjc6co00b={weeks={week1=[菱形俯卧撑、长刀、plyo弓箭步、plyo深蹲,单腿支架(每轮交替)、plyo弓箭步、俯卧撑和旋转、加权侧支架(每轮交替)、交替俯卧撑、胸部扩张器、菱形俯卧撑、俯卧撑俯卧撑、单臂侧俯卧撑、俯卧撑和旋转、t形俯卧撑、蜘蛛侠俯卧撑、宽臂俯卧撑、burpee俯卧撑]},title=title,类别=力量,长度=1,目标=目标}
程序.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;
    }    
}
mf98qq94

mf98qq941#

正如@reazmurshed所说,你就快到了。但是你不能在你的树中迭代一个级别。看来裁判是指着 subscriptions json中的节点。在这种情况下,你有用户,然后你有推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) {

  }
});
ajsxfq5m

ajsxfq5m2#

你就快到了。你只需要加上 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中检索数据。这是一个与这里相同的数据库结构的示例。

相关问题