4

そうする簡単な方法はありますか?入力されたテキストと同一の 1 つの要素を持つドロップダウンは冗長に見えるためです。

私のアダプターはシンプルです。コードは次のとおりです

AutoCompleteTextView autoCompleteTextViewAddress;
...
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(AvatarMainActivity.this, android.R.layout.simple_list_item_1, emailsSet.toEmailStringSet());
    autoCompleteTextViewAddress.setAdapter(adapter);

emailsSet.toEmailStringSet()文字列のセットを返します。

文字列セットと同じメールをautoCompleteTextViewAddress入力しても、1 つの要素を持つドロップダウンを表示できます。

4

2 に答える 2

1

醜い解決策ですが、うまくいきます:

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

    public CustomAutoCompleteTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public CustomAutoCompleteTextView(Context context, AttributeSet attrs)
    {
        super(context,attrs);
    }
    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context,attrs,defStyle);
    }
    @Override
    public boolean enoughToFilter()
    {
        boolean isEnough=(getThreshold()<=this.getText().length());

        if(isEnough)
        {
            if(this.getAdapter()!=null)
            {
                int itemsCount=0;
                int matchIndex=0;
                String txt = this.getText().toString();
                for (int i=0; i< this.getAdapter().getCount();i++) 
                {
                    String dat = (String)this.getAdapter().getItem(i);
                    if(dat.startsWith(txt))
                    {
                        itemsCount++;
                        matchIndex=i;
                    }
                }
                if(itemsCount == 1)
                {
                     if(((String)getAdapter().getItem(matchIndex)).equals(txt))
                     {
                         isEnough=false;
                     }

                }
            }
        }
        return isEnough;

    }


}

オリジナルの代わりにカスタム クラスを使用しますAutoCompleteTextView

オーバーライドenoughToFilterされた関数は、アダプターに一致するレコードが 1 つだけある場合にドロップダウンを非表示にします

于 2012-08-20T14:38:05.617 に答える
0

コードの種類によって異なります

しかし、これは提案を取得するSQLの例です

     If( (select count(*) from /* your code here */) > 1)
     Select /* field */ from /* your code again here */

そうすれば、2 つ以上の提案がある場合にのみ表示されます。

于 2012-08-15T13:25:14.747 に答える