过滤自定义适配器 IndexOutOfBoundsException

2023-11-21

我是安卓新手。

我的自定义适配器在过滤时导致异常。

这是我的代码。 私有类 DeptAdapter 扩展 ArrayAdapter 实现 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()


请记住getView()将查询该项目的大小superclass拥有,现在就是你起初调用超类构造函数时传递它,

super(context, textViewResourceId, items);

因此,超类不知道您在过滤时更改了大小。这意味着getCount()将返回数组的原始大小,可以理解,该大小比过滤后的数组大。

这意味着您应该覆盖getCount()方法,以便确保返回实际有效的大小:

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

如果要使用与 List 操作(例如获取)相关的其他方法,您还应该重写它们。 例如:

@Override
public Dept getItem (int pos){
     return items.get(pos);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

过滤自定义适配器 IndexOutOfBoundsException 的相关文章

随机推荐