如何使用自定义数组适配器修复AutoCompleteTextView中的“ publishResults返回空FilterResults”?

时间:2019-09-09 08:54:51

标签: java android autocompletetextview custom-arrayadapter

我正在设置一个AutoCompleteTextView,它将使用 Google Places API 过滤城市。

我创建了一个带有自定义过滤器的自定义ArrayAdapter:

PlacesAutoCompleteAdapter.java

public class PlacesAutoCompleteAdapter extends ArrayAdapter<String> implements Filterable{

private static final String TAG = "PlacesAutoComplete";
private ArrayList<String> mPlacesList;
private PlacesClient mPlacesClient;
private AutocompleteSessionToken mAutoCompleteSessionToken;

public PlacesAutoCompleteAdapter(@NonNull Context context, int resource) {
    super(context, resource);
    this.mPlacesList = new ArrayList<>();
    mPlacesClient = Places.createClient(context);
    mAutoCompleteSessionToken = AutocompleteSessionToken.newInstance();
}

@Override
public int getCount() {
    return mPlacesList.size();
}

@Nullable
@Override
public String getItem(int position) {
    return mPlacesList.get(position);
}

@NonNull
@Override
public Filter getFilter() {
    return new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence charSequence) {
            final FilterResults results = new FilterResults();

            if(charSequence != null){
                mPlacesList.clear();

                FindAutocompletePredictionsRequest mRequest = FindAutocompletePredictionsRequest.builder()
                        .setCountry("in")
                        .setTypeFilter(TypeFilter.ADDRESS)
                        .setSessionToken(mAutoCompleteSessionToken)
                        .setQuery(charSequence.toString())
                        .build();

                mPlacesClient.findAutocompletePredictions(mRequest).addOnSuccessListener(new OnSuccessListener<FindAutocompletePredictionsResponse>() {
                    @Override
                    public void onSuccess(FindAutocompletePredictionsResponse findAutocompletePredictionsResponse) {
                        for(AutocompletePrediction mPrediction : findAutocompletePredictionsResponse.getAutocompletePredictions()){
                            mPlacesList.add(mPrediction.getPrimaryText(null).toString());
                        }
                        results.values = mPlacesList;
                        results.count = mPlacesList.size();
                        Log.d(TAG, "onSuccess: Size of filter result: "+ results.count);
                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e(TAG, "onFailure: ",e );
                    }
                });
            }else{
                Log.e(TAG, "performFiltering: Null query");
            }
            return results;
        }

        @Override
        protected void publishResults(CharSequence charSequence, FilterResults filterResults) {

            List<String> filterList = (ArrayList<String>)filterResults.values;
            if (filterResults.count > 0) {
                clear();
                for (String place : filterList) {
                    add(place);
                }
                notifyDataSetChanged();
            }else{
                Log.e(TAG, "publishResults: Null " );
                notifyDataSetInvalidated();
            }
        }
    };
  }
}

SecondFragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_second, container, false);
    Places.initialize(view.getContext(), Api.GOOGLE_API_KEYS);
    mPlacesClient = Places.createClient(view.getContext());
    mPlacesAdapter = new PlacesAutoCompleteAdapter(view.getContext(), android.R.layout.simple_dropdown_item_1line);
    mLocation = view.findViewById(R.id.location_auto_complete);
    mLocation.setAdapter(mPlacesAdapter);
    mLocation.setThreshold(1);
    mLocation.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            mPlacesAdapter.getFilter().filter(charSequence);
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    });
    return view;
}

每当我输入任何字母时,它应该显示“自动完成”面板,但未显示,并且logcat显示:

2019-09-09 13:38:36.507 13500-13500/com.shashank.brahmansamaj E/PlacesAutoComplete: publishResults: Null 
2019-09-09 13:38:36.507 13500-13500/com.shashank.brahmansamaj E/PlacesAutoComplete: publishResults: Null 
2019-09-09 13:38:37.510 13500-13500/com.shashank.brahmansamaj D/PlacesAutoComplete: onSuccess: Size of Filter: 5
2019-09-09 13:38:37.510 13500-13500/com.shashank.brahmansamaj D/PlacesAutoComplete: onSuccess: Size of Filter: 10

现在,这意味着正在对数据进行过滤,但是publishResult()的{​​{1}}为空。

请帮助我哪里做错了。

0 个答案:

没有答案