带有Geocoder的Android AutoCompleteTextView

时间:2015-06-27 17:22:11

标签: android geolocation textview autocompletetextview textwatcher

我想将AutoCompleteTextViewGeocoder一起使用,但当我开始输入时,建议不会弹出。

我不明白为什么建议不弹出?这有什么解决方案吗?

这是我的代码:

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();
}

1 个答案:

答案 0 :(得分:1)

从我提供的代码中我可以看出,您可能忘记在正在使用的AutoCompleteTextView上设置阈值。阈值确定用户在建议出现之前必须键入的字符数;如果您没有设置阈值,则不会显示任何结果。

在设置适配器之前尝试执行此操作:

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();
    }
}

希望有所帮助!