在填充AutoCompleteTextView onTextChanged时更改ArrayAdapter

时间:2015-01-12 20:23:51

标签: android android-arrayadapter autocompletetextview

我发现了许多这样的问题,但没有回答。在键入ArrayList时是否可以更改适配器的AutoCompleteTextView

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

1 个答案:

答案 0 :(得分: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..." />