所以我有一个自定义适配器,它扩展了arrayadapter。我正在尝试使用searchview从我的arraylist中筛选,如果我键入第一个字母,它会被正确筛选,但当我键入第二个字母时,应用程序会崩溃,出现indexoutofbounds异常。
适配器已连接到listview。
适配器:
public class SearchActivityAdapter extends ArrayAdapter<String> implements Filterable {
Context context;
ArrayList<String> videoList;
ArrayList<String> filteredList;
public SearchActivityAdapter(@NonNull Context context, ArrayList<String> list) {
super(context, R.layout.search_activity_listitem);
this.context = context;
this.videoList = list;
this.filteredList = videoList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
ViewHolder_search viewholder;
if(convertView==null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.search_activity_listitem, parent, false);
viewholder = new ViewHolder_search(convertView);
convertView.setTag(viewholder);
viewholder.image = convertView.findViewById(R.id.search_view_image);
viewholder.videoName = convertView.findViewById(R.id.search_view_textview);
}
else{
viewholder = (ViewHolder_search) convertView.getTag();
}
Log.d("setting value for position---",String.valueOf(position));
String name = "";
name = filteredList.get(position);
name = name.substring(name.lastIndexOf("/") + 1);
viewholder.videoName.setText(name);
Glide
.with(context)
.asBitmap()
.centerCrop()
.load(filteredList.get(position))
.into(viewholder.image);
return convertView;
}
@Override
public int getCount() {
return videoList.size();
}
@NonNull
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if(constraint == null || constraint.length() == 0){
filterResults.count = filteredList.size();
filterResults.values = filteredList;
}
else
{
String query = constraint.toString().toLowerCase();
List<String> result = new ArrayList<>();
for (String name : filteredList) {
String original =name;
String tempname = name.substring(name.lastIndexOf("/")+1);
tempname = tempname.toLowerCase();
if (tempname.contains(query)) {
result.add(original);
}
}
filterResults.count = result.size();
filterResults.values = result;
}
return filterResults;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
filteredList = (ArrayList<String>) results.values;
Log.d("filtered list---", String.valueOf(filteredList));
notifyDataSetChanged();
}
};
}
}
日志:
2020-11-30 01:16:01.115 29689-29689/com.testapp.test3 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.testapp.test3, PID: 29689
java.lang.IndexOutOfBoundsException: Index: 10, Size: 10
at java.util.ArrayList.get(ArrayList.java:437)
at com.testapp.test3.SearchActivityAdapter.getView(SearchActivityAdapter.java:52)
at android.widget.AbsListView.obtainView(AbsListView.java:2471)
at android.widget.ListView.makeAndAddView(ListView.java:2052)
at android.widget.ListView.fillDown(ListView.java:786)
at android.widget.ListView.fillGap(ListView.java:750)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5511)
at android.widget.ListView.trackMotionScroll(ListView.java:1971)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3723)
at android.widget.AbsListView.onTouchMove(AbsListView.java:4158)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3964)
at android.view.View.dispatchTouchEvent(View.java:11840)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2994)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2659)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3000)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2673)
at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:592)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1977)
at android.app.Activity.dispatchTouchEvent(Activity.java:3405)
at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:548)
at android.view.View.dispatchPointerEvent(View.java:12095)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4966)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4761)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4259)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4329)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4287)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4429)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4295)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4486)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4259)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4329)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4287)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4295)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4259)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6854)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6828)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6783)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6966)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:194)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
2020-11-30 01:16:01.117 29689-29689/com.testapp.test3 E/AndroidRuntime: at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:181)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:6937)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:6989)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:925)
at android.view.Choreographer.doCallbacks(Choreographer.java:737)
at android.view.Choreographer.doFrame(Choreographer.java:663)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:911)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:192)
at android.app.ActivityThread.main(ActivityThread.java:6702)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:826)
我该怎么解决这个问题?
1条答案
按热度按时间hm2xizp91#
我不知道为什么,但问题是
ArrayAdapter<String
. 当我把它去掉并加上BaseAdapter
成功了。基本适配器代码: