0

This sounds easy but it has been hard for me. I have an autocomplete textview that shows user location address. I also implemented a places api to get addresses if user enters a different location other than their location. Everything is working like it is supposed to but the places result is still showing even when there is already an address. To reduce cost I would like to get address results only when the user enters an address. I made a global boolean and set it true when the text is changed like so:

autocomplete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                isTextEntered = true;  //to get autocomplete get results only after user enters text
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

Then I check if the boolean is true when I set my adapter as such:

if (isTextEntered) {
            autocomplete.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_results_list_item, R.id.tvSearchResultItem));
            autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final String selectedPlace = (String) parent.getItemAtPosition(position);
                    autocomplete.setText(selectedPlace);
                }
            });
        }

But doing this in oncreate method of my activity simply blocks the autocomplete from showing any places hint results. How can I accomplish my goal here? As always any help is much appreciated.

This is my custom adapter class:

class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {

        private ArrayList resultList;
        //private ArrayList<HashMap<String, Place>> results = new ArrayList<>();

        public GooglePlacesAutocompleteAdapter(Context context, int list, int textViewResourceId) {

            super(context, list, textViewResourceId);
        }

        @Override
        public int getCount() {
            return resultList.size();
        }

        @Override
        public String getItem(int index) {
            return resultList.get(index).toString();
        }

        //@Override
        //public HashMap<String, Place>  getItem(int index) {return results.get(index);}

        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        resultList = autocomplete(constraint.toString());
                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, FilterResults results) {
                    if (results != null && results.count > 0) {
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };
            return filter;
        }
    }
The_Martian
  • 3,083
  • 2
  • 26
  • 50
  • no need for `TextWatcher` and `OnItemClickListener`, it is done by `AutoCompleteTextView`, all you need is to provide filtered data to the adapter – pskink Mar 11 '16 at 10:00
  • @pskink I am implementing the TextWatcher and OnItemClickListener interfaces of the AutocompleteTextView class. I don't understand what you mean. – The_Martian Mar 11 '16 at 20:08
  • see my answer [here](http://stackoverflow.com/a/19860624/2252830) – pskink Mar 11 '16 at 20:28
  • do you know how to use it now? – pskink Mar 13 '16 at 04:54
  • @pskink Not really. I managed to stop it from showing the suggestions by passing a boolean to the constructor of the adapter class. But when pass true from within the onTextchanged, my app crashes because the getCount method of the adpater class. I would really appreciate it if you could give me a hint. Thanks – The_Martian Mar 14 '16 at 03:23
  • i showed you that no `onTextChanged` is needed – pskink Mar 14 '16 at 07:56

2 Answers2

1

Here is my idea to achieve this.

complete.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                  // 1. init autocomplete 
                  // 2. show autocomplete
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               // 1. get enter text
               // 2. update the datasource of autocomplete adapter
            }

            @Override
            public void afterTextChanged(Editable s) {
               // hide the autocomplete
            }
        });

Hope it help

Linh
  • 43,513
  • 18
  • 206
  • 227
  • I am using and a custom adapter class. Do you mean refactor that class. I will post that code now so you see how I am doing it. – The_Martian Mar 11 '16 at 19:22
0

What I will suggest to you is just refactor it as a method like here

public void setSelectedPlace() {
            autocomplete.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.layout.search_results_list_item, R.id.tvSearchResultItem));
            autocomplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    final String selectedPlace = (String) parent.getItemAtPosition(position);
                    autocomplete.setText(selectedPlace);
                }
            });
  }

And call it inside afterTextChanged method like here, and hide the autocomplete after that

@Override
 public void afterTextChanged(Editable s) {
     setSelectedPlace();
  // hide the autocomplete
 }
Shree Krishna
  • 7,883
  • 6
  • 34
  • 66
  • This completely hides the acotocomple textview. If I comment out the hiding part I don't get results after I enter text. – The_Martian Mar 11 '16 at 18:54