4

有没有简单的方法可以做到这一点?因为带有一个与输入文本相同的元素的下拉菜单看起来是多余的。

我的适配器很简单,这里是代码

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与字符串集中的电子邮件相同的电子邮件时,我仍然可以查看包含一个元素的下拉列表。

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当我们的适配器中只有一个匹配的记录时,覆盖的函数会隐藏下拉列表

于 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 回答