如何在Firebase Android中搜索Child的值 [英] How to search for a values of Child in firebase Android

查看:171
本文介绍了如何在Firebase Android中搜索Child的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  DatabaseReference databaseReference = mDatabase; 
String queryText =Hotel;
databaseReference.orderByChild(Coupon)
.startAt(queryText)
.endAt(queryText +\\\);




这里我附上了我用来获取子名优惠券,当我进入酒店查询下的优惠券。但我得到了空白。我应该得到Hotel1,Hotel2的对象。我是新的firebase。所以希望你的支持。提前感谢。



解决方案

在Web版本中,他们使用了一个名为ElasticSearch的东西,您可以尝试在这里阅读更多: https://firebase.googleblog.com/2014/01/但是对于Android而言,我认为没有任何功能可以像这样执行搜索。我会做的是查询所有记录,然后自己过滤它们:

$ p $ DatabaseReference databaseReference = mDatabase;
mDatabase.addValueEventListener(new ValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot val:dataSnapshot.getChildren()){
//我不确定你在寻找什么记录
//这就是如果你得到Key这是你的Coupon对象的记录ID
if(val.getKey( ).contains(Hotel)){
//根据记录
做你想做的事情}

//这是如果你正在查询酒店的孩子
if(val.child(hotel)。getValue(String.class).contains(Hotel)){
//根据记录做

}
}
@Override
public void onCancelled(FirebaseError firebaseError){

}

}


DatabaseReference databaseReference=mDatabase;
        String queryText="Hotel";
        databaseReference.orderByChild("Coupon")
                .startAt(queryText)
                .endAt(queryText+"\uf8ff");

Here I attached the code which I used to get child names of "Coupon" when I entered the "Hotel" query under the Coupon.But I got blank.I supposed to get Hotel1,Hotel2 object.I'm new to firebase.So hope your support .Thanks in advance.

解决方案

In the Web version, they use something called ElasticSearch, you could try to read more here: https://firebase.googleblog.com/2014/01/queries-part-2-advanced-searches-with.html

But for Android, I think there isn't any functionality to perform a search like that. What I would do is to query all the records then filter them myself:

DatabaseReference databaseReference = mDatabase;
mDatabase.addValueEventListener(new ValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    for(DataSnapshot val : dataSnapshot.getChildren()){
        //I am not sure what record are you specifically looking for
        //This is if you are getting the Key which is the record ID for your Coupon Object
        if(val.getKey().contains("Hotel")){
            //Do what you want with the record
        }

        //This is if your are querying for the hotel child
        if(val.child("hotel").getValue(String.class).contains("Hotel")){
            //Do what you want with the record
        }
    }
}
@Override
public void onCancelled(FirebaseError firebaseError) {

}

}

这篇关于如何在Firebase Android中搜索Child的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆