AutocompleteTextView建议始终选择建议中的第一项

时间:2018-12-30 08:51:38

标签: android android-adapter android-filterable

我正面临一个无法解决的问题。我创建了一个AutoCompleteTextView,它显示来自远程JSON数据的过滤建议。建议框会正常弹出并正确过滤。我有两个我无法理解的问题。

  1. 选择建议项时, AutoCompleteTextView始终设置为原始的第一项 建议数组,无论是否过滤数据。
  2. 在AutocompleteTextview中删除字符时,如果变成 空抛出空指针异常

我的自定义适配器的代码:

package tz.co.fsm.fas;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class office_locations_adapter extends ArrayAdapter<office_location_data> implements Filterable {
    private Context context;
    private List<office_location_data> items, tempItems, suggestions;
    int row_layout;

    public office_locations_adapter(Context context, int row_layout, List<office_location_data> items) {
        super(context, row_layout, items);
        this.context = context;
        this.row_layout = row_layout;
        this.items = items;
        tempItems = new ArrayList<>(items); 
        suggestions = new ArrayList<>();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(row_layout, parent, false);
        }

        office_location_data office = items.get(position);

        if (office != null) {
            TextView lbloffice = view.findViewById(R.id.rowtxtOfficeLocation);
            if (lbloffice != null) {
                lbloffice.setText(office.getOffice_location());
            }
        }

        return view;
    }

    // This is important as it returns the count of the new arraylist when we filter it.
    @Override
    public int getCount() {
        return items.size();
    }

    // Method to return the filter
    @Override
    public Filter getFilter() {
        return performFiletring;
    }

    // Create a new filter. Here we perform the filtering results and use this in the getFilter() method
    Filter performFiletring = new Filter() {
        @Override
        public CharSequence convertResultToString(Object resultValue) {
            String str = ((office_location_data) resultValue).getOffice_location();
            return str;
        }

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {
            // We have some text to search
            if (constraint != null) {
                // CLear the suggestions array
                suggestions.clear();
                // Find the rows which match and add them to suggestions
                for (office_location_data offices : tempItems) {
                    if (offices.getOffice_location().toLowerCase().contains(constraint.toString().toLowerCase())) {
                        suggestions.add(offices);
                    }
                }
                // Pass the filter results to the next step publish results
                FilterResults filterResults = new FilterResults();
                filterResults.values = suggestions;
                filterResults.count = suggestions.size();
//                System.out.println(filterResults.count);
                return filterResults;
            } else {
                return new FilterResults();
            }
        }

        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            items = (List) results.values;
            if (results.count > 0) {
                //suggestions = (office_location_data) results.values;
                notifyDataSetChanged();
            } else {
                notifyDataSetInvalidated();
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

发现了问题。 publishResults()方法是错误的:

@Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            List<office_location_data> items = (ArrayList<office_location_data>) results.values;
            if (results != null && results.count > 0) {
                clear();
                for (office_location_data offices : suggestions) {
                    add(offices);
                    notifyDataSetChanged();
                }
            } else {
                notifyDataSetInvalidated();
            }
        }

这就是我的方法。