I want to use AutoCompleteTextView
with Geocoder
, but when I start typing suggestions do not pop up.
I do not get why suggestions do not pop up? Is there any solution to this?
Here is my code:
ArrayList<String>addressList = new ArrayList<String>();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, addressList);
autoComplete.setAdapter(adapter);
autoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
getAddressInfo(getActivity(), location, s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void getAddressInfo(Context context, Location location, String locationName){
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
try {
List<Address> a = geocoder.getFromLocationName(locationName, 5);
for(int i=0;i<a.size();i++){
String city = a.get(0).getLocality();
String country = a.get(0).getCountryName();
String address = a.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
addressList.add(address+", "+city+", "+country);
}
} catch (IOException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}