嘗試從Firebase中的子類別中檢索數據,並將其保存到名爲testList的列表中。事情是,清單最終是空的。我究竟做錯了什麼?從Firebase獲取數據時列表變空
{
"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) {
}
});
我也試過這個,而不是上述這didn't工作要麼...
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) {
}
});
我創建了一個鍛鍊類:
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;
}
}
爲什麼會這樣幫助? –