过滤自定义适配器IndexOutOfBoundsException异常 [英] filtering custom adapter IndexOutOfBoundsException

查看:134
本文介绍了过滤自定义适配器IndexOutOfBoundsException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手在机器人。

筛选条件时,我的自定义适配器事业例外。

My custom Adapter cause exception when filtering.

这是我的code。 私有类DeptAdapter扩展ArrayAdapter实现可筛选{

here is my code. private class DeptAdapter extends ArrayAdapter implements Filterable {

    private ArrayList<Dept> items;
    private ArrayList<Dept> mOriginalValues;

    public DeptAdapter(Context context, int textViewResourceId, ArrayList<Dept> items) {
            super(context, textViewResourceId, items);
            this.items = items;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;

        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.item_listview_2line, null);
        }
        Dept d = items.get(position);
        if (d != null) {
                TextView tt = (TextView) v.findViewById(R.id.toptext);
                TextView bt = (TextView) v.findViewById(R.id.bottomtext);
                if (tt != null){
                    tt.setText(d.dept_nm);                            
                }
                if(bt != null){
                    bt.setText(d.dept_cd);
                }
        }
        return v;
    }

    @Override
    public Filter getFilter() {
        Filter filter = new Filter() {

            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint,FilterResults results) {

                items = (ArrayList<Dept>) results.values; // has the filtered values
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }

            }
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();        // Holds the results of a filtering operation in values
                ArrayList<Dept> FilteredArrList = new ArrayList<Dept>();

                if (mOriginalValues == null) {
                    mOriginalValues = new ArrayList<Dept>(items); // saves the original data in mOriginalValues
                }

                if (constraint == null || constraint.length() == 0) {

                    // set the Original result to return  
                    results.count = mOriginalValues.size();
                    results.values = mOriginalValues;
                } else {
                    constraint = constraint.toString().toLowerCase();
                    for (int i = 0; i < mOriginalValues.size(); i++) {
                        Dept d = mOriginalValues.get(i);
                        if (d.dept_cd.toLowerCase().startsWith(constraint.toString()) || d.dept_nm.toLowerCase().startsWith(constraint.toString())) {
                            FilteredArrList.add(d);
                        }
                    }
                    // set the Filtered result to return
                    results.count = FilteredArrList.size();
                    results.values = FilteredArrList;
                }
                return results;
            }
        };
        return filter;
    }
}

class Dept
{
    String dept_cd; 
    String dept_nm; 
    public Dept(String dept_cd, String dept_nm)
    {
        this.dept_cd = dept_cd;
        this.dept_nm = dept_nm;
    }
    public String toString()
    {
        return this.dept_nm+ "(" + this.dept_cd +")" ;
    }
}

帮助我的人.... 我不明白为什么getView()方法被调用更多的则items.size()

help me anyone.... I can't understand why getView() method was invoked more then items.size()

推荐答案

请注意, getView()将查询的项目的大小,该了,这现在,是你的的通过它调用父类的构造函数时,

Keep in mind that getView() will query the size of the items that the superclass has, which right now, is what you originally passed it when calling the superclass constructor,

super(context, textViewResourceId, items);

因此​​,超不知道你改变大小的时候,你已经过滤。这意味着 getCount将()将返回数组,这是可以理解的比你的滤波阵列较大的原始大小。

Therefore, the superclass doesn't know that you've changed the size when you have filtered. This means getCount() will return the original size of the array, which is understandably larger than your filtered array.

这意味着你应该重写 getCount将()方法让你确保你返回的实际有效尺寸:

This means You should override the getCount() method so you're sure that you're returning the actual valid size:

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

您也应该覆盖相关的列表操作的其他方法(如获取),如果你要使用它们。 例如:

You should also override the other methods related to the List operations (such as getting) if you are going to be using them. Eg:

@Override
public Dept getItem (int pos){
     return items.get(pos);
}

这篇关于过滤自定义适配器IndexOutOfBoundsException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆