1

for the past few days now I have being trying to implement a suggest as u type feature in my firebase android app with no success..So here is my json sample data with all unique keys no collisions

'
"ServiceTags" : {
                "Android Apps Developer" : "Android Apps Developer",
                "Arduino Expert" : "Arduino Expert",
                "Blackberry Apps Developer" : "Blackberry Apps Developer",
                "IOS Apps Developer" : "IOS Apps Developer",
                "Symbian Apps Developer" : "Symbian Apps Developer",
                "Windows Apps Developer" : "Windows Apps Developer"
}`

So,what I want is when a user starts typing for example the character 'A' in an Android Autocomplete textview I want to pull out all nodes whose key starts with the Character 'A'.

Below is what I have tried so far but it always returns the ENTIRE ServiceTags.Very frustrating...

          AutoCompleteTextView  serviceAutocompleteView;

          serviceAutocompleteView.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) 
            {

             //This is where I do the filtering as the user types

            if (StringUtils.isNotEmpty(charSequence.toString())) {
                pullOutTagsSuggestions(charSequence.toString());
            }


        }

        @Override
        public void afterTextChanged(Editable editable) {

        }

    });

    @SuppressWarnings("unchecked")
public void pullOutTagsSuggestions(final String text) {

    Query tagsReferences - FirebaseDatabase.getInstance().getReference().child("ServiceTags").orderByKey().startAt(StringUtils.capitalize(text));
    tagsReferences.addChildEventListener(new ChildEventListener() {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            if (dataSnapshot != null) {

                String serviceString = dataSnapshot.getValue(String.class);

                if (StringUtils.isNotEmpty(serviceString){
                     suggestions.add(serviceString);
                }

                ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.select_dialog_item, suggestions);
                serviceAutocompleteView.setAdapter(arrayAdapter);
            }
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

Where the StringUtils.capitalize() function capitalizes only the first character of text and the StringUtils.isNotEmpty() function is meant to make sure the text is neither null nor "";

I will sincerely appreciate any help.Thanks

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Wan
  • 97
  • 8

1 Answers1

1

I think you need to add .endAt statement to your query, which will limit the results. Since you typed A, the query returns everything starting with A. If you type W, i think you would only get one response.

From this

Query tagsReferences = FirebaseDatabase.getInstance().getReference().child("ServiceTags").orderByKey().startAt(StringUtils.capitalize(text));

To this

Query tagsReferences = FirebaseDatabase.getInstance().getReference().child("ServiceTags").orderByKey().startAt(StringUtils.capitalize(text)).endAt(StringUtils.capitalize(text)+ "\uf8ff");

See https://stackoverflow.com/a/39715702/2330272

Community
  • 1
  • 1
Barry MSIH
  • 3,525
  • 5
  • 32
  • 53