gpt4 book ai didi

android - 过滤自定义适配器 IndexOutOfBoundsException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:55:30 25 4
gpt4 key购买 nike

我是 android 的新手。

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

这是我的代码。私有(private)类 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() 将查询父类(super class) 拥有的项目的大小,这就是您最初 在调用父类(super class)构造函数时传递它,

super(context, textViewResourceId, items);

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

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

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

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

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

关于android - 过滤自定义适配器 IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15194835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com