How to search for a value in firebase Android

前端 未结 7 1796
孤城傲影
孤城傲影 2020-12-01 05:37

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

相关标签:
7条回答
  • 2020-12-01 06:17

    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)
        {
    
        }
    };
    
    0 讨论(0)
  • 2020-12-01 06:24

    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) });
    
    0 讨论(0)
  • 2020-12-01 06:29
        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) {}
    });
    
    0 讨论(0)
  • 2020-12-01 06:32

    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.

    0 讨论(0)
  • 2020-12-01 06:33

    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
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-01 06:37

    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;
    } }
    
    0 讨论(0)
提交回复
热议问题