尝试从我的Firebase中名为category的子项中检索数据,并将其保存在名为testList的List中。问题是该列表最终为空。我究竟做错了什么?
{
"elements" : {
"element_id_0" : {
"description" : "While laying flat on your back, arms at 90 degrees etc",
"name" : "Bench press"
},
"element_id_1" : {
"description" : "Incline the backboard 30 degrees and etc etc",
"name" : "Incline Dumbbell Press"
}
},
"workouts" : {
"workout_id_0" : {
"category" : "Heavy Chest and Arms",
"elements" : {
"element_id_0" : "true",
"element_id_1" : "true"
}
},
"workout_id_1" : {
"category" : "Light Back and Shoulders",
"elements" : {
"element_id_0" : "true"
}
}
}
}
我使用的一些应用程序代码。
myFirebaseRef = new Firebase("https://myfirebase.firebaseio.com/workouts");
myFirebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
testList.add(child.child("category").getValue().toString());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
我也试过这个,而不是上面的,这也不起作用......
Firebase myFirebaseRef;
List<String> testList;
long räknare;
private static final String TAG = "Anders";
// ArrayAdapter<Integer> timeAdapter;
// ArrayAdapter<CharSequence> timeAdapter;
List<String> messages;
Integer[] sets= {1,2,3,4,5};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Firebase.setAndroidContext(this);
setContentView(R.layout.activity_start);
myFirebaseRef = new Firebase("https://myfirebaseio.com/workouts");
myFirebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Log.d("There are " + dataSnapshot.getChildrenCount() + " workouts");
for (DataSnapshot workoutSnapshot: dataSnapshot.getChildren()) {
Workout workout = workoutSnapshot.getValue(Workout.class);
testList.add(workout.getcategory());
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
我为此创建了一个Workout类:
package org.example.anders.eazy;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* Created by Anders on 2016-02-28.
*/
public class Workout {
// private String workouts;
@JsonIgnore
private String category;
private String elements;
private Workout() {}
// empty default constructor, necessary for Firebase to be able to deserialize blog posts
/* Workout(String category,String elements){
this.category=category;
this.elements=elements;
}
*/
// public String getWorkout() {
//
// return workouts;
// }
public String getcategory() {
return category;
}
}
答案 0 :(得分:1)
也许再加一个孩子
myFirebaseRef = new Firebase("https://myfirebase.firebaseio.com/workouts");
myFirebaseRef.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child : snapshot.getChildren()) {
testList.add(child.child.child("category").getValue().toString()); <------- Here
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
答案 1 :(得分:1)
你无法真正按照自己的要求行事:
如果您想要检索一个类别并且您知道路径,那很容易
ref = new Firebase("https://myfirebase.firebaseio.com/workouts/workout_id_1/category");
ref.addListenerForSingleValueEvent etc etc
那将返回那个(也就是那个)路径的值,即“Light Back and Shoulders”
如果要获取所有类别,则需要读入所有训练节点,然后从快照中提取类别。
此外,请注意您之前问题的答案也包含对此问题的回答: