2017-04-10 35 views
0

我是firebase的新手,想要獲取包含「category」鍵中特定鍵的所有商店記錄,如何從上述數據中檢索結果WHERE CATEGORY = CATEGORY_09關於孩子的密鑰查詢

我的數據庫:

{ 
    "Shops" : { 
    "shop_01" : { 
     "adress" : "Calle Belisario Domínguez 3,", 
     "category" : "category_08", 
     "description" : "One Description", 
     "email" : "[email protected]", 
     "facebook" : "www.facebook.com/profile", 
     "horary" : "8 am - 10pm", 
     "image" : "https://firebasestorage.googleapis.com/v0/b/...", 
     "latitude" : 19.561588, 
     "longitude" : -97.240889, 
     "name" : "Grupo Culinario Luiggi's", 
     "phone" : 28128253532 
    } 
    },"shop_02" : { 
     "adress" : "Rafael Murillo Vidal", 
     "category" : "category_09", 
     "description" : "One description", 
     "email" : "[email protected]", 
     "facebook" : "www.facebook.com/profile", 
     "horary" : "8 am - 10pm", 
     "image" : "https://firebasestorage.googleapis.com/v0/b/...", 
     "latitude" : 19.561588, 
     "longitude" : -97.240889, 
     "name" : "Grupo Culinario Luiggi's", 
     "phone" : 232321532 
    } 
} 

我希望能得到您的幫助,非常感謝你

回答

2
DatabaseReference database = FirebaseDatabase.getInstance().getReference("shops"); 
Query category = database.orderByChild("category").equalTo(CATEGORY_09); 

category.addChildEventListener(new ChildEventListener() { 
    @Override 
    public void onChildAdded(DataSnapshot dataSnapshot, String s) { 
     // get the data here of CATEGORY_09 
    } 
    ... 
} 

我認爲商店爲您的商店表的引用

+0

由於這是我需要什麼 –

0

每次檢查類別,就可以解決問題。

public class Main_Activity extends AppCompatActivity { 

    private DatabaseReference mList; 
    private FirebaseDatabase mFirebaseInstance; 

    @Override 
    protected void onCreate(@Nullable Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mFirebaseInstance = FirebaseDatabase.getInstance(); 

     mList = mFirebaseInstance.getReference("Shops"); 


     mList.addListenerForSingleValueEvent(new ValueEventListener() { 

      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 


       for (DataSnapshot child : dataSnapshot.getChildren()) { 
         if(child.child("category").getValue().toString().equals("category_09"){ 
            String adress = child.child("adress").getValue().toString(); 
            String facebook = child.child("facebook").getValue().toString(); 
            String latitude = child.child("latitude").getValue().toString(); 
            String name = child.child("name").getValue().toString(); 
            String longitude = child.child("longitude").getValue().toString(); 
            //then just add them to a list or db 
         } 


       }  
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 


    } 

}