0
votes

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;
        }
    }
2
no need for TextWatcher and OnItemClickListener, it is done by AutoCompleteTextView, all you need is to provide filtered data to the adapterpskink
@pskink I am implementing the TextWatcher and OnItemClickListener interfaces of the AutocompleteTextView class. I don't understand what you mean.The_Martian
see my answer herepskink
do you know how to use it now?pskink
@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. ThanksThe_Martian

2 Answers

1
votes

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

0
votes

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
 }