2017-07-21 53 views
-3

My json data structure如何利用火力UI

誰能幫我找回從每一個孩子除了數據向doctor_profile檢索火力數據。 我需要爲每個孩子創建單獨的課程,還是有另一種方法來解決這個問題。 TIA

+0

這裏https://stackoverflow.com/questions/45235341/how-to檢查我的答案-create-json-object-using-com-google-gson-class/45235660#45235660 –

回答

0
FirebaseDatabase database = FirebaseDatabase.getInstance(); 
DatabaseReference myRef = database.getReference("addiction psychiatrist"); 

myRef.addListenerForSingleValueEvent(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 
       for (DataSnapshot snapshot: dataSnapshot.getChildren()) { 
        Psychiatrist psychiatrist = snapshot.getValue(Psychiatrist.class); 

        //now you can add it to your list (collection) or do what you want 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 

,當然你必須有類代表你的對象,因此,例如:

@IgnoreExtraProperties 
public class Psychiatrist { 

    private String name; 
    private String city; 
    private String hospital; 

    public Psychiatrist() { 
     // required empty constructor 
    } 

    public Psychiatrist(String name, String city, String hospital) { 
     this.name = name; 
     this.city = city; 
     this.hospital = hospital; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getCity() { 
     return city; 
    } 

    public String getHospital() { 
     return hospital; 
    } 
} 
+0

我是否需要爲每個孩子創建類? –

+0

別人是否有相同的字段(姓名,城市,醫院)?如果是,那麼可能你可以爲每個孩子創建一個班級,但是你應該增加一個額外的描述孩子類型的字段(精神科醫生,心臟病學家......)。我希望你明白了 –