Autocompletetextview arrayadapter中的过滤器无法获得所需的结果

时间:2015-11-22 10:55:04

标签: android android-arrayadapter autocompletetextview

我的代码有什么问题?

下面的

是AutoCompleteTextView的自定义适配器,我覆盖基本方法,并且还覆盖了filter方法,但它没有给我所需的结果。

    public class AirportCodesAdapter extends ArrayAdapter<AirportCodes> {

    public Context mContext;
    public ArrayList<AirportCodes> arrayList;
    public int layoutResourceId;

    public AirportCodesAdapter(Context mContext, int layoutResourceId, ArrayList<AirportCodes> arrayList) {

        super(mContext, layoutResourceId, arrayList);

        this.layoutResourceId = layoutResourceId;
        this.mContext = mContext;
        this.arrayList = arrayList;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        try {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_row_airports_codes, parent, false);
            }
            TextView Code = (TextView) convertView.findViewById(R.id.code_text_list_row_airport_codes);
            TextView airport = (TextView) convertView.findViewById(R.id.airport_name_list_row_airport_codes);

            Code.setText(arrayList.get(position).getCode());
            airport.setText(arrayList.get(position).getAirport());
        } catch (NullPointerException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    @Override
    public AirportCodes getItem(int position) {

        return arrayList.get(position);
    }

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

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public Filter getFilter() {
        return nameFilter;
    }


    Filter nameFilter = new Filter() {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults filterResults = new FilterResults();
            ArrayList<AirportCodes> tempList = new ArrayList<AirportCodes>();
            //constraint is the result from text you want to filter against.
            //objects is your data set you will filter from
            if (constraint != null && arrayList != null) {
                int length = arrayList.size();
                int i = 0;
                while (i < length) {
                    for (AirportCodes customer : arrayList) {
                        if(customer.getCode().toString().trim().toUpperCase().startsWith(constraint.toString().trim().toUpperCase()) {
                            tempList.add(customer);
                        }
                    }

//                    AirportCodes item = arrayList.get(i);
//                    //do whatever you wanna do herem
//                    //adding result set output array
//                    tempList.add(item);
                    i++;
                }
                //following two lines is very important
                //as publish result can only take FilterResults objects
                filterResults.values = tempList;
                filterResults.count = tempList.size();
            }
            return filterResults;

        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            arrayList = (ArrayList<AirportCodes>) results.values;
            if (results.count > 0) {
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
//
//
//            if (results != null && results.count > 0) {
//                suggestions = (ArrayList<AirportCodes>) results.values;
//                notifyDataSetChanged();
//            } else {
//                notifyDataSetInvalidated();
//            }

        }

    };
}

我花了6个多小时试图查找为什么代码只显示机场列表,过滤器不会过滤结果。

编辑: 所需的行为是只显示在AutoCompleteTextView中具有(startsWith)键入文本的文本,以便我可以将其发送到上一个活动。 我编辑了我的代码以使其更合适 我希望我的问题很清楚。

0 个答案:

没有答案