2

アダプターのカスタム レイアウトで AutoCompleteTextView を使用しています。問題は、デフォルトのレイアウトのように、結果を一度に 1 つだけに制限する方法がわからないことです。高さを制限することは可能ですが、すべての画面で機能するとは限りません。ご清聴ありがとうございました。

これは私の activity_main レイアウトにあります。

<AutoCompleteTextView android:id="@+id/autotext" 
android:layout_width="0dp" 
android:layout_height="wrap_content"
android:minWidth="480dp" 
android:layout_weight="1" 
android:maxLength="23" 
android:maxLines="1" 
android:textColor="#000000" />

これがアダプターのレイアウトです。

<TextView android:id="@+id/textpop" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="17sp"/>
4

3 に答える 3

2

このコードを見てください。これは基本的に、SDKArrayAdapterクラスのコードに小さな変更を加えたものです (これはカウントされません)。に 1 つの提案のみを表示する場合はAutoCompleteTextView、リンクからクラスに小さな変更を加えて、次のperformFiltering()ようなメソッドにすることができます。

// ... the rest of that method
if (newValues.size() > 1) { // if we have more than an item in the list
    // get the first suggestion
    T oneItem = newValues.get(0);
    // empty the suggestion list
    newValues.clear();
    // add the only suggestion.
    newValues.add(oneItem);
}
results.values = newValues;
results.count = newValues.size();
于 2012-12-03T18:44:15.277 に答える
0

Filterable インターフェイスを実装する独自の AutoCompleteTextView アダプターを作成するか、既存のアダプターを拡張しますが、getFilter() メソッドをオーバーライドし、独自のフィルター実装を作成してから、フィルター ロジックをカスタマイズして、好きな数の結果を返すことができます。

参照用に ArrayAdpater の ArrayFilter 実装を参照できます。

于 2016-04-30T15:49:38.163 に答える
0

上記の回答は不十分であり、リンクは無効になっています。

これを行う最も簡単な方法として、カスタム アダプターを作成し、単純にカウントを 1 に強制します。

class SingleArrayAdapter extends ArrayAdapter<String> {

        public SingleArrayAdapter(Context context, int resource, String[] objects) {
            super(context, resource, objects);
        }

        @Override
        public int getCount() {
            return 1;
        }
    }
于 2015-06-02T10:22:25.940 に答える