0

新しいデータを取得してフィルタリングする 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;
        }
    }

多くの宛先を取得する 2 文字を入力して下にスクロールすると、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>
4

1 に答える 1

0

わかりました私は解決策を管理したので、ここに行きます:

カスタム フィルターの代わりに、asynctask が新しいデータを取得した後に onPostExcecute で更新する通常のアダプターを使用します。その結果、新しいアダプターを作成し、autocompletetextview に設定します。また、トラフィックを抑えるために、2 文字でのみ新しい検索を実行します。

秘訣は、ユーザーが入力を開始したときにデータがないため、オートコンプリートに実際に何かを表示させることです。新しいアダプターを設定して .showDropdown を実行すると機能しましたが、ユーザーが最初の 2 の後にさらに文字を入力するため、データは実際に入力されたデータにフィルターされませんでした。

これは、autocompletetextview テキストをテキストに設定することで解決され、フィルタリングとドロップダウン データが強制的に更新されました。ここで重要なのは、afterTextChange のテキストを使用することです (私は textchange リスナーを使用しています) setText を使用し、次に setSelection(newText.length()) を呼び出してカーソルを正しい場所に配置します。

私がここに提出した問題は、データを新しい方法で処理しているため、解決されていません。フィルター内でオートコンプリート データを処理することは、スレッド化されており、作業時に不変データを想定しているため、良い考えではないようです。私が言うことができるように。

于 2011-06-24T08:22:42.833 に答える