首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AutoCompleteTextView未按预期工作

AutoCompleteTextView未按预期工作
EN

Stack Overflow用户
提问于 2014-01-13 08:53:40
回答 5查看 532关注 0票数 0

我正在开发一个应用程序,在这个应用程序中我正在使用AutoCompleteTextView,并且面临的问题不多。请查找以下问题的详细情况。

数据中有以下值:

1) Manish Logan Jain

2) M.J. (Logan Fern)

3)洛根

问题:

1)当用户搜索Manish时,Manish Logan Jain显示为建议。但是当用户进入Logan时,就不会返回任何结果。

2)当用户输入Logan时,我希望第二个值显示为建议,但目前,建议列表没有显示任何内容。

3)当用户进入ogan时,我希望能显示建议3。目前,它没有显示。

AutoCompleteView xml:

AutoCompleteTextView

代码语言:javascript
复制
    android:id="@+id/autoCompleteTextView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:ems="10"
    android:hint="@string/enter_user_name" >

    <requestFocus />
</AutoCompleteTextView>

填充数据的Java代码:

代码语言:javascript
复制
    List<String> namesList = new ArrayList<String>(stops);
    namesList.add("Manish Logan Jain");
    namesList.add("Logan");
    namesList.add("M. J. (Logan Fern)");

    ArrayAdapter<String> namesSuggestion = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, namesList);
    AutoCompleteTextView textView = (AutoCompleteTextView)                       findViewById(R.id.autoCompleteTextView1);
     textView.setAdapter(namesSuggestion);
     textView.setThreshold(1);

有人遇到过类似的问题吗?如果是,那么解决这一问题的可能办法是什么?

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2014-01-13 09:09:46

对ACTV使用CursorAdapter,对自定义筛选使用setFilterQueryProvider(FilterQueryProvider) (对过滤数据使用MatrixCursor )

编辑:示例FilterQueryProvider

代码语言:javascript
复制
class FQP extends LinkedList<String> implements FilterQueryProvider {
    @Override
    public Cursor runQuery(CharSequence constraint) {
        if (constraint == null) {
            return null;
        }
        Log.d("TAG", "runQuery " + constraint);

        String lowerConstraint = constraint.toString().toLowerCase();
        String[] columns = {
                "_id", "name"
        };
        int id = 0;
        MatrixCursor c = new MatrixCursor(columns);
        for (String name : this) {
            String lowerName = name.toLowerCase();
            if (lowerName.indexOf(lowerConstraint) != -1) {
                c.newRow().add(id++).add(name);
            }
        }
        return c;
    }
};

在onCreate中用以下方法测试它:

代码语言:javascript
复制
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
AutoCompleteTextView actv = new AutoCompleteTextView(this);
String[] from = {"name"};
int[] to = {android.R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_dropdown_item_1line, null, from, to);
FQP fqp = new FQP();
fqp.add("Manish Logan Jain");
fqp.add("Logan");
fqp.add("M. J. (Logan Fern)");
adapter.setFilterQueryProvider(fqp);
actv.setAdapter(adapter);
actv.setThreshold(1);
ll.addView(actv);
setContentView(ll);
票数 2
EN

Stack Overflow用户

发布于 2014-01-13 09:02:25

使用实现可过滤的自定义适配器。在getFilter方法中,根据需要使用String.contains()。

请检查这个链接。

票数 2
EN

Stack Overflow用户

发布于 2014-01-13 09:04:12

你要找的是不能直接做的。但我建议你使用MultiCompleteTextView。

参考以下链接:http://www.c-sharpcorner.com/uploadfile/manish1231/autocomplete-and-multicomplete-textview-in-mono-for-android/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21087111

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档