我有一个用于获取新数据并过滤它的 AutoCompleteTextView 的自定义 performFiltering:
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults filterResults = new FilterResults();
if (null != constraint && constraint.length() >= 2) {
ArrayList<Destination> temporaryDestination = updateArray();
filterResults.count = temporaryDestination.size();
filterResults.values = temporaryDestination;
return filterResults;
} else {
if (destinations != null) {
filterResults.count = destinations.size();
filterResults.values = destinations;
}
return filterResults;
}
}
如果我键入两个检索大量目的地的字母并向下滚动,我会得到一个 FC 和以下堆栈跟踪:
06-22 14:44:07.756: ERROR/AndroidRuntime(2188): FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.AbsListView$LayoutParams
at android.widget.AutoCompleteTextView.buildDropDown(AutoCompleteTextView.java:1350)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1140)
at android.widget.AutoCompleteTextView.onKeyDown(AutoCompleteTextView.java:714)
at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
at android.view.View.dispatchKeyEvent(View.java:3855)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
如果我最多返回 5 个结果,它就可以正常工作(我只需添加以下内容):
if (null != constraint && constraint.length() >= 2) {
ArrayList<Destination> temporaryDestination = updateArray();
if (temporaryDestination.size()>5) {
temporaryDestination = new ArrayList<Destination>(temporaryDestination.subList(0, 5));
}
filterResults.count = temporaryDestination.size();
filterResults.values = temporaryDestination;
我已在AutoCompleteTextView的第 1350 行的 Android 源代码中将错误跟踪到以下内容,该代码执行以下操作:
if (view != null) {
LinearLayout.LayoutParams hintParams =
(LinearLayout.LayoutParams) view.getLayoutParams();
otherHeights = view.getMeasuredHeight() + hintParams.topMargin
+ hintParams.bottomMargin;
}
}
但是,我不明白为什么当结果大于 5 并且您开始滚动时,此代码会收到错误的类。将结果限制为 5 的解决方案很难看,因为我觉得真正的问题仍然存在。有人有什么建议吗?
这是自动完成项的 xml 布局(但没有自定义下拉实现):
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#000">
</TextView>
</RelativeLayout>