ArrayAdpater を使用した ListView があります。ListView をフィルタリングしようとすると、ArrayAdpater が正しい値を取得しています。しかし、ListView は常に ArrayOutOfBounds Exception をスローするため、それらを表示できません。たとえば、私のリストには 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)