在填充AutoCompleteTextView的onTextChanged事件中更改ArrayAdapter

3
我发现有很多类似的问题,但是没有答案。当输入 AutoCompleteTextView 时,是否可以更改适配器的 ArrayList
 private void setUpAutocomplete() {
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_dropdown_item_1line, COUNTRIES);
    final AutoCompleteTextView textView = autocompleteTV;
    textView.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) {
            adapter.clear();
            ArrayList<String> locations=gAPI.autocomplete(s.toString());
            adapter.addAll(locations);
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });

    textView.setAdapter(adapter);
}

https://dev59.com/JWUq5IYBdhLWcg3wPuFQ - Ollie Strevel
你想告诉我唯一可能的选择是创建编辑文本框吗? - Ondrej Tokar
AutoCompleteTextView继承自EditText,不仅如此,我还为您提供了一个简单的选项=) - Ollie Strevel
使用TextWatcher与AutocompleteTextView是一个错误,你应该使用已经内置在适配器中的过滤机制。 - pskink
为了用户搜索的自动完成..谷歌API位置。但我在谷歌文档中找到了一些东西,我将尝试它。 - Ondrej Tokar
显示剩余3条评论
1个回答

1

是的,有。以下代码从Google Places API检索地点并填充ArrayAdapter,该适配器随AutocompleteTextView中的文本更改而更改。

// declare this variable globally in your activity so that it can be
//referenced and updated in the inner class (public void onResult(...))
private ArrayAdapter<String> adapter; 

AutoCompleteTextView locationText = (AutoCompleteTextView) findViewById(R.id.location_text);
    adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_dropdown_item_1line);
    locationText.setAdapter(adapter);

    locationText.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) {

            String query = s.toString();

//mMap is a GoogleMap object. Alternatively, you can initialize a
//LatLngBounds object directly through its constructor
            LatLngBounds bounds = mMap.getProjection().getVisibleRegion().latLngBounds;

            AutocompleteFilter filter = new AutocompleteFilter.Builder()
                    .setTypeFilter(AutocompleteFilter.TYPE_FILTER_NONE)
                    .build();

            PendingResult<AutocompletePredictionBuffer> autocompleteResult =
                    Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient, query,
                            bounds, filter);

            autocompleteResult.setResultCallback(new ResultCallback<AutocompletePredictionBuffer>() {
                @Override
                public void onResult(@NonNull AutocompletePredictionBuffer autocompletePredictions) {
                    for (int i = 0; i < autocompletePredictions.getCount(); i++) {
                        adapter.add(autocompletePredictions.get(0).getFullText(null).toString());
                    }
                    autocompletePredictions.release();
                    adapter.notifyDataSetChanged();
                }
            });

        }

以下是AutocompleteTextView的XML代码:
<AutoCompleteTextView
        android:id="@+id/location_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:hint="Search location..." /> 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接