查询孩子的钥匙

时间:2017-04-10 20:15:06

标签: android firebase firebase-realtime-database

我是firebase的新手,想要获取包含“category”键中特定键的所有商店记录,如何从上面的数据中检索结果WHERE CATEGORY = CATEGORY_09

我的数据库:

{
  "Shops" : {
    "shop_01" : {
      "adress" : "Calle Belisario Domínguez 3,",
      "category" : "category_08",
      "description" : "One Description",
      "email" : "luiggis@gmail.com",
      "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" : "lp@gmail.com",
      "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 个答案:

答案 0 :(得分: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
    }
    ...
}

我假设商店作为商店表的参考

答案 1 :(得分: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) {

           }
       });


    }

}