PerformFiletering获取null参数

时间:2015-12-27 18:50:35

标签: android autocompletetextview

我有AutoCompletTextView,我想要为此应用一些自定义文件字符串,我有这个代码

public class ATCAdapter extends ArrayAdapter<String> implements Filterable {

    ArrayList<String> _items = new ArrayList<String>();
    ArrayList<String> orig = new ArrayList<String>();

    public ATCAdapter(Context context, int resource, ArrayList<String> items) {
        super(context, resource, items);

        for (int i = 0; i < items.size(); i++) {
            orig.add(items.get(i));
        }
    }

    @Override
    public int getCount() {
        if (_items != null)
            return _items.size();
        else
            return 0;
    }

    @Override
    public String getItem(int arg0) {
        return _items.get(arg0);
    }


    @Override

    public Filter getFilter() {
        Filter filter = new Filter() {
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {

                if(constraint != null) {
                    Log.d("Constraints", constraint.toString());
                }

                FilterResults oReturn = new FilterResults();

                /*  if (orig == null){
                    for (int i = 0; i < items.size(); i++) {
                        orig.add(items.get(i));
                    }
                  }*/
                String temp;
                int counters = 0;
                if (constraint != null){

                    _items.clear();
                    if (orig != null && orig.size() > 0) {
                        for(int i=0; i<orig.size(); i++)
                        {
                            temp = orig.get(i).toUpperCase();

                            if(temp.startsWith(constraint.toString().toUpperCase()))
                            {

                                _items.add(orig.get(i));
                                counters++;

                            }
                        }
                    }
                    Log.d("REsult size:" , String.valueOf(_items.size()));
                    if(counters != 0)
                    {
                        _items.clear();
                        _items = orig;
                    }
                    oReturn.values = _items;
                    oReturn.count = _items.size();
                }
                return oReturn;
            }


            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                if(results != null && results.count > 0) {
                    notifyDataSetChanged();
                }
                else {
                    notifyDataSetInvalidated();
                }

            }

        };

        return filter;

    }

}

这就是我为

设置适配器的方法
AutoCompleteTextView    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);

        ArrayAdapter adapter = new ATCAdapter(this, android.R.layout.simple_list_item_1, new ArrayList<>(Arrays.asList(languages)));
        autoCompleteTextView.setThreshold(1);
        autoCompleteTextView.setAdapter(adapter);

现在的问题是我总是在performFiletring()中得到null参数,这是它发生的原因吗?

1 个答案:

答案 0 :(得分:1)

IHMO,你的代码在

有问题
if(counters != 0)
{
    _items.clear();
    _items = orig;
}

我建议您按以下方式更新代码:

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                if (constraint != null) {
                    Log.d("Constraints", constraint.toString());
                }
                FilterResults oReturn = new FilterResults();
                String temp;
                int counters = 0;
                if (constraint != null && constraint.length() > 0) {
                    _items.clear();
                    if (orig != null && orig.size() > 0) {
                        for (int i = 0; i < orig.size(); i++) {
                            temp = orig.get(i).toUpperCase();
                            if (temp.startsWith(constraint.toString().toUpperCase())) {
                                _items.add(orig.get(i));
                                counters++;
                            }
                        }
                    }
                    Log.d("Result size:", String.valueOf(_items.size()));
                    if (counters == 0) {
                        _items = new ArrayList<>(orig);
                    }
                    oReturn.values = _items;
                    oReturn.count = _items.size();
                } else {
                    _items = new ArrayList<>(orig);
                    oReturn.values = _items;
                    oReturn.count = _items.size();
                }
                return oReturn;
            }

和构造函数:

   public ATCAdapter(Context context, int resource, ArrayList<String> items) {
        super(context, resource, items);

        for (int i = 0; i < items.size(); i++) {
            orig.add(items.get(i));
        }
        _items = new ArrayList<>(orig); // ADD THIS LINE
    }