2016-03-02 37 views
0

嘗試從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; 
    } 
} 

回答

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) { 

    } 
}); 
+0

爲什麼會這樣幫助? –

1

你c an't真的做的正是你所問的:

如果你想獲取一個類別,你知道的路徑,這很容易

ref = new Firebase("https://myfirebase.firebaseio.com/workouts/workout_id_1/category"); 
ref.addListenerForSingleValueEvent etc etc 

將返回在那個值(和只)路徑,即「輕盈背部」

如果要獲取所有類別,則需要讀取所有鍛鍊節點,然後從快照中提取類別。

另外,請注意回答你剛纔的問題也包含了這個問題的答案:

Getting objects out from Firebase(Json)

+0

是嘗試使用該代碼,但沒有真正理解如何在我的情況下使用它。但我非常感謝您的建議:) –

+0

查看Firebase關於[檢索數據]的網站指南(https://www.firebase.com/docs/android/guide/retrieving-data.html)。您所尋求的答案在閱讀事件類型部分。 – Jay

+0

是的,我讀了很多,但似乎沒有得到它的工作.. –