Reputation: 3767
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;
}
}
Upvotes: 0
Views: 700
Reputation: 8562
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
}
Upvotes: 0
Reputation: 60923
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
Upvotes: 1