自动完成的 TextView 在 Android。以首字母完成。

huangapple go评论61阅读模式
英文:

AutoComplete TextView Android. Complete with first letters

问题

自动完成文本视图中的适配器类似于这样:

  • -苹果
    -梨
    -橙子
    -香蕉
    -葡萄

目前的工作方式是,如果我键入“p”,下拉列表中将显示苹果、梨和葡萄,因为它们都匹配,但我只希望它显示梨,因为它是唯一以字母“p”开头的单词。所以问题是:如何告诉自动完成的下拉列表仅显示以我搜索的内容开头的单词匹配,而不显示包含在其他位置但不是首字母的单词。
例如,如果我键入“pe”,我只想显示梨,而不是葡萄,因为梨以该字符串开头。
这是我的代码

ArrayList<String> data = dbHelper.getDataAsString();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
autoCompleteTv.setAdapter(adapter);
autoCompleteTv.setThreshold(2);
英文:

The adapter that I have in the autoCompleteTextView is something like this:

  • -apple
    -pear
    -orange
    -banana
    -grape

The way it's working now if I type "p", in the dropDown list will appear apple, pear and grape because they make a match but I just want it to show pear because is the only word which starts with p. So, the question is: How can I tell the autoComplete's dropDownList to show matches with the words that start with what I'm searching on and not show words that contains it in some other position that is not the first.
For example, if I type "pe" I want to show only pear and not grape because pear starts with that string.
This is my code

ArrayList&lt;String&gt; data = dbHelper.getDataAsString();
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
autoCompleteTv.setAdapter(adapter);
autoCompleteTv.setThreshold(2);

答案1

得分: 1

这只是作为参考。我在制作后刚测试了这段代码,应该可以正常工作。

假设您的列表仅包含字符串类型。

private String[] searchableList = {"apple", "pear", "grape"};

我制作了这个函数,它接受 query 并返回新格式化的字符串列表。

private List<String> filterQuery(String query) {
    // 这会将所有与查询匹配的字符串项筛选出来
    List<String> filterList = new ArrayList<>();

    // 遍历列表中的每个项
    for (String currentString : searchableList) {
        // 确保所有内容都是小写。
        String myString = currentString.toLowerCase(Locale.getDefault());
        // 从字符串中获取前两个字符,您可以根据需要进行更改。
        String formatTitle = myString.substring(0, 2);
        // 如果查询与当前字符串匹配,则将其添加到 filterList 中
        if (formatTitle.contains(query)) {
            filterList.add(formatTitle);
        }
    }
    return filterList;
}

让我知道进展如何。

英文:

Take this as a reference. I just tested this code after making, it should work fine.

Assume your list was of only type strings.

private String[] searchableList = {&quot;apple&quot;, &quot;pear&quot;, &quot;grape&quot;};

I made this function which takes query and returns newly formatted list of strings.

    private List&lt;String&gt; filterQuery(String query) {
        // This takes all strings items which are valid with query
        List&lt;String&gt; filterList = new ArrayList&lt;&gt;();
        
        // Looping into each item from the list
        for (String currentString : searchableList) {
            // Make sure everything is lower case.
            String myString = currentString.toLowerCase(Locale.getDefault());
            // Take first two characters from the string, you may change it as required.
            String formatTitle = myString.substring(0, 2);
            // If query matches the current string add it to filterList
            if (formatTitle.contains(query)) {
                filterList.add(formatTitle);
            }
        }
        return filterList;
    }

Let me know how it goes.

huangapple
  • 本文由 发表于 2020年9月19日 03:59:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63962112.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定