0

I have an edit text. I am watching the text changing at each letter and for each letter added I want to query a request to firebase, giving me the results that have this letters added. So I tried the following solution offered here -

How to do a simple search in string in Firebase database?

and it doesn't seem to work.

I have these JSON profiles in my database:


{
  "002xUGm1uRXDYc8JpEy7AKFWFyy2" : {
    "contest_uid" : "-LRoyQlTw5vo3CIE3tfb",
    "created_at" : 1543726522,
    "device_id" : "354020096008438",
    "email" : "koushickroy321@gmail.com",
    "is_suspended" : false,
    "is_uploading" : false,
    "last_login_at" : 1543726522,
    "last_upload_started_at" : 0,
    "name" : "gold rush eren patym cash patym",
    "photoUrl" : "*"
  },
  "00IumwilishPwTgPUXX6GmveP5w2" : {
    "contest_uid" : "-LRoyQlTw5vo3CIE3tfb",
    "created_at" : 1543214441,
    "device_id" : "865758030661505",
    "email" : "khushyouheart@gmail.com",
    "is_suspended" : false,
    "is_uploading" : false,
    "last_login_at" : 1543214441,
    "last_upload_started_at" : 0,
    "name" : "Khushal Chouhan",
    "photoUrl" : "*"
  },
  "00TRPNqSi1R41F4SUuhHOgHBdkN2" : {
    "contest_uid" : "-LO3Eg7zf1xh9RGDUy0e",
    "created_at" : 1541355818,
    "device_id" : "357149084088198",
    "is_suspended" : false,
    "is_uploading" : false,
    "last_login_at" : 1541355818,
    "last_upload_started_at" : 0,
    "name" : "Gagan Singh Gaur",
    "photoUrl" : "*"
  },


and here is what I have already tried to do -


mExploreSearchEditText.addTextChangedListener(new TextWatcher() {
      @Override
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

      }

      @Override
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


      }

      @Override
      public void afterTextChanged(Editable editable) {
          if (editable.length() < 1) {
            mExploreSearchRecommendedProfiles.setVisibility(View.VISIBLE);
            fetchSixRandomProfiles();
          } else {
            mExploreSearchRecommendedProfiles.setVisibility(View.GONE);
            fetchNewListFromEditable(editable);
          }
      }
    });
  }

  private void fetchNewListFromEditable(Editable editable) {
    String text = editable.toString();
    Query query = FirebaseDatabase.getInstance().getReference().child("profiles").orderByChild("").startAt(text).endAt(text +"\uf8ff");
    query.addListenerForSingleValueEvent(new ValueEventListener() {
      @Override
      public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        if (dataSnapshot.exists()) {
          String users = dataSnapshot.getValue(String.class);
          Timber.tag("similar users").d(users); // <-- I have never got to this point 
        } else {
          Toast.makeText(App.getAppContext(), "snapshot does not exist", Toast.LENGTH_SHORT).show();
        }
      }

      @Override
      public void onCancelled(@NonNull DatabaseError databaseError) {
        Timber.tag("similar users error - " + databaseError.getMessage());
      }
    });
  }


What I need is for every letter added, query the entire "profiles" table, if the name of the profile contains the entire charsequence from start then I need the name and the photoURL. What would be the best way to achieve this result ?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Alon Shlider
  • 1,187
  • 1
  • 16
  • 46
  • @FrankvanPuffelen Thank you - I have edited my post. Could you please re open this thread so people can answer? – Alon Shlider Sep 24 '19 at 15:27
  • This makes no sense: `orderByChild("")`. It honestly should throw an exception, but definitely won't work. What child property do you want to order on? Specify that property. Probably that is `orderByChild("name")`, but only you know for sure. – Frank van Puffelen Sep 24 '19 at 15:33
  • Also note that there are many things coming together here. While I can probably help you get your query working, I'm not always the best to help with Android UI elements. It is best if you minimize the number of technologies that one needs to know to answer the question. I.e. if you can reproduce your problem with hard-coded search strings, and with just logging output, that'll make it more likely that someone with just database knowledge can help. – Frank van Puffelen Sep 24 '19 at 15:36

1 Answers1

1

Something like this should do the trick:

Query query = FirebaseDatabase.getInstance().getReference().child("profiles").orderByChild("name").startAt(text).endAt(text +"\uf8ff");
query.addListenerForSingleValueEvent(new ValueEventListener() {
  @Override
  public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()) {
      for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
        String users = snapshot.child("name").getValue(String.class);
        Timber.tag("similar users").d(users);
      }
    } else {
      Toast.makeText(App.getAppContext(), "snapshot does not exist", Toast.LENGTH_SHORT).show();
    }
  }

  @Override
  public void onCancelled(@NonNull DatabaseError databaseError) {
    Timber.tag("similar users error - " + databaseError.getMessage());
  }
})

Changes:

  • Use orderByChild("name") to order/filter the profiles on the value of the name property.
  • In onDataChange loop over the children, since a query can have multiple results.
  • You need to get the correct property value from each child snapshot, so snapshot.child("name").getValue(String.class)

What I haven't done yet:

  • Show the list of users in the search results. If you need that, have a look at creating your own adapter, and calling notifyDataSetChanged().
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807