0

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();
}
Yogesh Mangaj
  • 2,890
  • 4
  • 29
  • 41
Zookey
  • 2,456
  • 12
  • 40
  • 66

1 Answers1

1

From what I can tell from the code you provided it looks like you may have forgotten to set a threshold on the AutoCompleteTextView that you are using. The threshold determines how many characters a user must type before the suggestions will appear; if you do not set a threshold no results will ever be shown.

Try doing this before setting your adapter:

public void setupAutoCompleteTextView(AutoCompleteTextView autoCompleteTextView) {
    ArrayAdapter<String>   adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, addressList);
    autoCompleteTextView.setThreshold(1);
    autoCompleteTextView.setAdapter(adapter);

    autoCompleteTextView.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(MainActivity.this, s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
}

private void getAddressInfo(Context context, 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(i).getLocality();
            String country = a.get(i).getCountryName();
            String address = a.get(i).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();
    }
}

Hope that helps!

Zookey
  • 2,456
  • 12
  • 40
  • 66
aaemman
  • 21
  • 3
  • I copied your code into a github repo (https://github.com/aaemman/AutoCompleteTextViewExampleForZookey) and was able to get the autoComplete Suggestions to come up after adding the threshold call. I would like to mention thought that the implementation of Geocoder (which I am not extremely familiar with) does not populate addressList with a very compete list of suggestions; have you tried filling the list with known strings to verify that the issue is not with your implementation of Geocoder? – aaemman Jun 28 '15 at 06:33
  • Hey, thank you very much. Please edit your answer and post the code, it works but with one minor downside. At least for me, pop up shows only when threshold is 1. If I put any other value like 2,3,4 it does not work. Does it work for you? – Zookey Jun 28 '15 at 09:32
  • And what about changing threshold to other values? – Zookey Jun 28 '15 at 17:09
  • sorry about the late response; you should be fine to just increase the value in the threshold declaration; I was able to get this to work with the code posted in this answer; if you are still having trouble with it I would suggest you try testing with a list of known strings before hooking into geocoder. – aaemman Jul 06 '15 at 15:29