How to search for a value in firebase Android

2019-01-07 13:59发布

问题:

I am saving data as follows in the Firebase:

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

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);
                    }
                });
            }
        });

回答1:

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

You can however search items whose title begins with #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:

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


回答2:

This is working with the 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:

    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:

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:

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:

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.

Here's a related question and the official docs.



回答6:

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) });