当前位置: 首页 > 面试题库 >

如何遍历Android中的Firebase结构?

仲孙默
2023-03-14
问题内容

我需要遍历Firebase架构以获取每个的数据workouts并在RecyclerView中显示它。现在,我无法使用侦听器遍历Firebase中的Schema,也无法通过名为的POJO类来获取它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 = [Diamond
Pushups,Jackknives,Plyo Lunges,Plyo
Squats,单腿板(备用)每轮),Plyo刺针,俯卧撑和旋转,配重侧板(每轮备用),交替俯卧撑板,扩胸器,钻石俯卧撑,潜水轰炸俯卧撑,单臂俯卧撑,高架俯卧撑,俯卧撑和旋转,T俯卧撑,蜘蛛侠俯卧撑,宽臂俯卧撑,Burpee俯卧撑]},标题=印度锻炼,类别=力量,长度=
1,目标=此锻炼可以由BABUJI完成},-KDnhRwHjssOejrqyenP = {weeks = {week1 =
[高膝盖,跳起的杰克,坐在墙壁上,俯卧撑,仰卧起坐,加高俯卧撑,蹲坐,Tricep俯卧在椅子上,木板,高位膝盖就位,滑刺,俯卧撑和旋转,侧板(每轮交替),交替式俯卧撑板,扩胸器,钻石俯卧撑,潜水轰炸机俯卧撑,对接踢腿,卧式三头肌举高,单臂侧俯卧撑,高架手臂拍打,高架俯卧撑,动力圈,俯卧撑和旋转,T俯卧撑,反向木板,蜘蛛侠俯卧撑,T上升,三头肌俯卧撑,墙壁俯卧撑,宽臂俯卧撑,Burbures]},标题=你好锻炼,类别=力量,长度=
1,目标=此锻炼可以在电话上进行! }}

03-28 08:52:53.967 23584-23584 / com.android.sam I /
System.out:{-KDnfjROKeFAL9wccsxY = {weeks = {week1 =
[高膝盖,下蹲,肺,钻石俯卧撑,卧式肱三头肌训练器]] },标题=下午HitT,类别=移动性,长度=
1,目标=下午体重锻炼},-KDps90Dn6XtJc6Co00b = {weeks = {week1 = [Diamond
Pushups,Jackknives,Plyo Lunges,Plyo
Squats,单腿木板(每人备用)圆形),Plyo刺,俯卧撑和旋转,配重侧板(每轮交替),交替俯卧撑板,扩胸器,钻石俯卧撑,潜水轰炸俯卧撑,单臂侧俯卧撑,高架俯卧撑,俯卧撑俯卧撑,T俯卧撑,蜘蛛侠俯卧撑,宽臂俯卧撑,Burpee俯卧撑]},标题=标题,类别=强度,长度=
1,目标=目标}}

程式库

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指向subscriptionsJSON中的节点。在此之下,您有用户,然后有推送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) {

  }
});


 类似资料:
  • 问题内容: 我具有以下JSON结构: 如何使用JavaScript进行迭代? 问题答案: 取自jQuery docs:

  • 问题内容: 我具有以下JSON结构: 如何使用JavaScript进行迭代? 问题答案: 取自jQuery docs:

  • 问题内容: 有什么不同的方法可以循环遍历mysql结果集?我是PHP和MySQL的新手,所以我正在寻找简单的循环方法以及有关所提供代码如何工作的解释。 问题答案: 这是一个完整的示例: http://php.net/manual/en/mysqli-result.fetch- array.php 连接 选择数据库 进行查询 循环处理结果并获取数组以获取行

  • 问题内容: 这是我正在使用的代码: 在我的视图中,这是我要查看的值: 想象一下,我查询类似的东西,并且返回一个结果集,例如: 如何遍历每个值并输出呢? 这里的文档没有用,因为我尝试了应该存在的方法,但是解释器给我一个错误,指出这些方法不存在。我使用了错误的文档吗? http://www.tmtm.org/en/mysql/ruby/ 谢谢! 问题答案: 如果您使用的是mysql2 gem,那么您应

  • 问题内容: 我有一个Firebase资源,其中包含几个对象,我想使用Swift对其进行迭代。我期望的工作如下(根据Firebase文档) https://www.firebase.com/docs/ios- api/Classes/FDataSnapshot.html#//api/name/children 因此,看来Swift遍历Firebase返回的NSEnumerator对象存在问题。 非常

  • 问题内容: 我有一个Map如下所示的bean: 这ftqSet适合以下数据结构: 在我的测试JSP文件中,我一直在尝试使用来访问数据 : 但是它没有输出${f.feedId}。为什么会这样呢?我将如何访问该结构的各个元素,以便创建一个漂亮的表? 问题答案: 的每次迭代中给出了一个实例,它反过来又和getValue()方法。这与在普通Java中进行操作类似。 例如 在的情况下, 的回报, ,所以你需