我有一个带有 ArrayAdpater 的 ListView。我尝试过滤 ListView 并且我的 ArrayAdpater 得到了正确的值。但我无法显示它们,因为 ListView 总是抛出 ArrayOutOfBounds 异常。例如,我的列表包含 16 个名称。其中3个以字母“l”开头。适配器中的我的 ItemsArray 仅包含 3 个名称,但它不会停止尝试填充列表,并且我得到一个 ArrayOutOfBounds 异常。
我评论了发生异常的代码。
class FriendListAdapter extends ArrayAdapter<Friend> implements Filterable {
private ArrayList<Friend> items;
ArrayList<Friend> mOriginalValues; // Original Values
public FriendListAdapter(Context context, int textViewResourceId, ArrayList<Friend> 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)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.addfriendtoeventlistrow, null);
}
final Friend o = items.get(position); //<-- Here comes the ArrayOutOfBounds because position > array.size()
if (o != null) {
TextView tt = (TextView) v.findViewById(R.id.friendname);
ImageView icon = (ImageView) v.findViewById(R.id.friendprofileimage);
final CheckBox selectBox = (CheckBox) v.findViewById(R.id.selectFriend);
selectBox.setClickable(false);
selectBox.setFocusable(false);
selectBox.setFocusableInTouchMode(false);
if(selectBox != null){
selectBox.setChecked(o.getSelected());
if(icon != null){
icon.setImageDrawable(o.getFriendProfileImage());
if (tt != null) {
tt.setText(o.getFriendName()); }
}
}
}
return v;
}
@Override
public Filter getFilter() {
Filter filter = new Filter() {
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint,FilterResults results) {
items = (ArrayList<Friend>) results.values; // has the filtered values
for(int i=0;i<items.size();i++){
Log.i("FilteredItems",items.get(i).getFriendName());
}
notifyDataSetChanged(); // notifies the data with new filtered values
}
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults(); // Holds the results of a filtering operation in values
ArrayList<Friend> FilteredArrList = new ArrayList<Friend>();
if (mOriginalValues == null) {
mOriginalValues = new ArrayList<Friend>(items); // saves the original data in mOriginalValues
}
/********
*
* If constraint(CharSequence that is received) is null returns the mOriginalValues(Original) values
* else does the Filtering and returns FilteredArrList(Filtered)
*
********/
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++) {
String data = mOriginalValues.get(i).getFriendName();
if (data.toLowerCase().startsWith(constraint.toString())) {
FilteredArrList.add(mOriginalValues.get(i));
}
}
// set the Filtered result to return
results.count = FilteredArrList.size();
results.values = FilteredArrList;
for(int i=0;i<FilteredArrList.size();i++){
Log.i("FilteredArrList",FilteredArrList.get(i).getFriendName());
}
}
return results;
}
};
return filter;
}
这就是我创建适配器和列表的方式
m_friends = new ArrayList<Friend>();
this.m_adapter = new FriendListAdapter(getActivity(), R.layout.addfriendtoeventlistrow, m_friends);
friendList = (ListView)view.findViewById(R.id.addfriendsList);
friendList.setAdapter(m_adapter);
这是例外
> 01-19 10:59:22.719: E/AndroidRuntime(3138): FATAL EXCEPTION: main
01-19 10:59:22.719: E/AndroidRuntime(3138): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 3
01-19 10:59:22.719: E/AndroidRuntime(3138): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-19 10:59:22.719: E/AndroidRuntime(3138): at java.util.ArrayList.get(ArrayList.java:304)
01-19 10:59:22.719: E/AndroidRuntime(3138): at com.flashback.flashbackapp.FriendListAdapter.getView(AddFriendsToEventDialog.java:252)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.AbsListView.obtainView(AbsListView.java:2019)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.ListView.onMeasure(ListView.java:1155)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.View.measure(View.java:12881)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1385)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.LinearLayout.measureVertical(LinearLayout.java:676)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.LinearLayout.onMeasure(LinearLayout.java:557)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.View.measure(View.java:12881)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.View.measure(View.java:12881)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.View.measure(View.java:12881)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.View.measure(View.java:12881)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4802)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.widget.FrameLayout.onMeasure(FrameLayout.java:297)
01-19 10:59:22.719: E/AndroidRuntime(3138): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2111)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.View.measure(View.java:12881)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.os.Looper.loop(Looper.java:137)
01-19 10:59:22.719: E/AndroidRuntime(3138): at android.app.ActivityThread.main(ActivityThread.java:4441)
01-19 10:59:22.719: E/AndroidRuntime(3138): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 10:59:22.719: E/AndroidRuntime(3138): at java.lang.reflect.Method.invoke(Method.java:511)
01-19 10:59:22.719: E/AndroidRuntime(3138): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-19 10:59:22.719: E/AndroidRuntime(3138): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-19 10:59:22.719: E/AndroidRuntime(3138): at dalvik.system.NativeStart.main(Native Method)