如何在firebase Android中搜索值

时间:2022-10-29 13:06:12

I am saving data as follows in the Firebase:

我在Firebase中保存数据如下:

如何在firebase Android中搜索值

I want to find all records that have #Yahoo in their title. What will be the exact query for that?

我想找到所有标题中都有#Yahoo的记录。对此的确切查询是什么?

I am confused with the random key created. I am not sure how to handle this so i am posting it here.

我对创建的随机密钥感到困惑。我不知道如何处理这个,所以我在这里发布它。

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));
        Query queryRef = firebase.orderByKey().startAt("#" + mHashTag).endAt("#" + mHashTag + "\uf8ff");

        queryRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                mTasksList.clear();
                mAdapter.notifyDataSetChanged();
                for (DataSnapshot task : dataSnapshot.getChildren()) {
                    mTasksList.add(task.getValue(TaskModel.class));
                }
                mAdapter.notifyItemRangeInserted(0, mTasksList.size());
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {
                mSwipeToRefresh.post(new Runnable() {
                    @Override
                    public void run() {
                        mSwipeToRefresh.setRefreshing(false);
                    }
                });
            }
        });

6 个解决方案

#1


17  

You cannot search for any item whose title contains #Yahoo. See: How to perform sql "LIKE" operation on firebase?

您无法搜索标题包含#Yahoo的任何项目。请参阅:如何在firebase上执行sql“LIKE”操作?

You can however search items whose title begins with #Yahoo:

但是,您可以搜索标题以#Yahoo开头的项目:

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));

Query queryRef = firebase.orderByChild("title").startAt("#" + mHashTag)

To make this work well, you have to add an index to your Firebase rules:

为了更好地工作,您必须为Firebase规则添加索引:

"rules": {
  "userTasks": {
    "$email": {
      ".indexOn": "title" // index tasks in their title property
    }
  }
}

#2


4  

This is working with the Google Firebase.

这适用于Google Firebase。

DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
    Query query = mFirebaseDatabaseReference.child("userTasks").orderByChild("title").equalTo("#Yahoo");
    query.addValueEventListener(valueEventListener);

ValueEventListener valueEventListener = new ValueEventListener()
{
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)
    {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
        {
            //TODO get the data here

        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError)
    {

    }
};

#3


3  

    DatabaseReference mDatabase =FirebaseDatabase.getInstance().getReference("userTasks");
    mDatabase.orderByChild("title").equalTo("#Yahoo").addListenerForSingleValueEvent(new ValueEventListener() {
               @Override
               public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
       ///Here you can get all data 
    }
    @Override
     public void onCancelled(DatabaseError databaseError) {}
});

#4


0  

Try the following code

请尝试以下代码

DatabaseReference mDatabaseRef =FirebaseDatabase.getInstance().getReference("userTasks");

  Query query=mDatabaseRef.orderByChild("title").equalTo("#Yahoo");

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot data:dataSnapshot.getChildren()){


                Records models=data.getValue(Records.class);
                String latitude=models.getLatitude();
                String longitude=models.getLongitude();

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I have declared only two strings here, latitude and longitude. If you want to load more values you need to add that also.

我在这里只声明了两个字符串,纬度和经度。如果要加载更多值,还需要添加。

Then create a new class Records as follows

然后创建一个新类记录如下

public class Records {

public String latitude,longitude;

public Records()
{

}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
} }

#5


0  

Answer : simply do following query :

答:只需执行以下查询:

databaseReference.orderByChild('_searchLastName')
 .startAt(queryText)
 .endAt(queryText+"\uf8ff");

We can combine startAt and endAt to limit both ends of our query. The following example finds all dinosaurs whose name starts with the letter b:

我们可以结合startAt和endAt来限制查询的两端。以下示例查找名称以字母b开头的所有恐龙:

curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&startAt="b"&endAt="b\uf8ff"&print=pretty'

The \uf8ff character used in the query above is a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with a b.

上面查询中使用的\ uf8ff字符是Unicode范围内的一个非常高的代码点。因为它是Unicode之后的大多数常规字符,所以查询匹配以b开头的所有值。

Here's a related question and the official docs.

这是一个相关的问题和官方文档。

#6


0  

I know this is late but this solution worked for me.

我知道这已经晚了但这个解决方案对我有用。

 getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Video.VideoColumns.DATA + "=?" , new String[]{ paths.get(i) });

#1


17  

You cannot search for any item whose title contains #Yahoo. See: How to perform sql "LIKE" operation on firebase?

您无法搜索标题包含#Yahoo的任何项目。请参阅:如何在firebase上执行sql“LIKE”操作?

You can however search items whose title begins with #Yahoo:

但是,您可以搜索标题以#Yahoo开头的项目:

Firebase firebase = new Firebase(Constants.FIREBASE_URL_USER_TASKS).child(Utils.encodeEmail(unProcessedEmail));

Query queryRef = firebase.orderByChild("title").startAt("#" + mHashTag)

To make this work well, you have to add an index to your Firebase rules:

为了更好地工作,您必须为Firebase规则添加索引:

"rules": {
  "userTasks": {
    "$email": {
      ".indexOn": "title" // index tasks in their title property
    }
  }
}

#2


4  

This is working with the Google Firebase.

这适用于Google Firebase。

DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference();
    Query query = mFirebaseDatabaseReference.child("userTasks").orderByChild("title").equalTo("#Yahoo");
    query.addValueEventListener(valueEventListener);

ValueEventListener valueEventListener = new ValueEventListener()
{
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)
    {
        for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
        {
            //TODO get the data here

        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError)
    {

    }
};

#3


3  

    DatabaseReference mDatabase =FirebaseDatabase.getInstance().getReference("userTasks");
    mDatabase.orderByChild("title").equalTo("#Yahoo").addListenerForSingleValueEvent(new ValueEventListener() {
               @Override
               public void onDataChange(DataSnapshot dataSnapshot) {
    for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
       ///Here you can get all data 
    }
    @Override
     public void onCancelled(DatabaseError databaseError) {}
});

#4


0  

Try the following code

请尝试以下代码

DatabaseReference mDatabaseRef =FirebaseDatabase.getInstance().getReference("userTasks");

  Query query=mDatabaseRef.orderByChild("title").equalTo("#Yahoo");

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot data:dataSnapshot.getChildren()){


                Records models=data.getValue(Records.class);
                String latitude=models.getLatitude();
                String longitude=models.getLongitude();

            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

I have declared only two strings here, latitude and longitude. If you want to load more values you need to add that also.

我在这里只声明了两个字符串,纬度和经度。如果要加载更多值,还需要添加。

Then create a new class Records as follows

然后创建一个新类记录如下

public class Records {

public String latitude,longitude;

public Records()
{

}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
} }

#5


0  

Answer : simply do following query :

答:只需执行以下查询:

databaseReference.orderByChild('_searchLastName')
 .startAt(queryText)
 .endAt(queryText+"\uf8ff");

We can combine startAt and endAt to limit both ends of our query. The following example finds all dinosaurs whose name starts with the letter b:

我们可以结合startAt和endAt来限制查询的两端。以下示例查找名称以字母b开头的所有恐龙:

curl 'https://dinosaur-facts.firebaseio.com/dinosaurs.json?orderBy="$key"&startAt="b"&endAt="b\uf8ff"&print=pretty'

The \uf8ff character used in the query above is a very high code point in the Unicode range. Because it is after most regular characters in Unicode, the query matches all values that start with a b.

上面查询中使用的\ uf8ff字符是Unicode范围内的一个非常高的代码点。因为它是Unicode之后的大多数常规字符,所以查询匹配以b开头的所有值。

Here's a related question and the official docs.

这是一个相关的问题和官方文档。

#6


0  

I know this is late but this solution worked for me.

我知道这已经晚了但这个解决方案对我有用。

 getContentResolver().delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        MediaStore.Video.VideoColumns.DATA + "=?" , new String[]{ paths.get(i) });