如何在 Android 中遍历 Firebase 结构?
How to Traverse a Firebase Structure in Android?
我需要遍历 Firebase 架构以获取每个 workouts
的数据并将其显示在 RecyclerView 中。
现在我无法使用侦听器遍历 Firebase 中的架构并通过名为 Program
.
的 POJO Class 获取它
JSON 来自 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" ]
}
}
}
}
}
听众代码
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;
}
}
你快到了。您只需添加 addListenerForSingleValueEvent
监听器和您的引用,以迭代与引用节点关联的数据。这是如何完成的示例。
在你的例子中,让我们声明一个名为 ProgramList
的 class
public class ProgramList {
public List<Program> 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 documentation for retrieving data in android again. This is an example。
正如@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) {
}
});
我需要遍历 Firebase 架构以获取每个 workouts
的数据并将其显示在 RecyclerView 中。
现在我无法使用侦听器遍历 Firebase 中的架构并通过名为 Program
.
JSON 来自 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" ]
}
}
}
}
}
听众代码
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;
}
}
你快到了。您只需添加 addListenerForSingleValueEvent
监听器和您的引用,以迭代与引用节点关联的数据。这是如何完成的示例。
在你的例子中,让我们声明一个名为 ProgramList
public class ProgramList {
public List<Program> 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 documentation for retrieving data in android again. This is an example。
正如@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) {
}
});