通过搜索视图搜索文本

时间:2016-10-30 15:13:12

标签: android searchview

我想在包含大量数据的textview中搜索单词。为了实现它,我在操作栏中添加了一个搜索选项,并尝试使用setOnQueryTextListener。但是我无法在文本视图上使用方法tv.getFilters()。filter()。此方法适用于阵列适配器。 您能否建议如何在文本视图中搜索文本

感谢您的帮助!!

1 个答案:

答案 0 :(得分:0)

首先在onQueryTextChange()上调用此方法。见下文。

@Override
        public boolean onQueryTextChange(String s) {
           filterListByPattern(s, List);// List is TextView Large amount of data convert each word as a list item.
            return true;
        }

在filterListByPattern()中,

 private void filterListByPattern(String s, ArrayList<String> mainList) 
{
    for(int i = 0; i < mainList.size(); i++)
    {
        String word = mainList.get(i);
        if(word.toLowerCase().toString().contains(s.toLowerCase().toString()))// if search word found in list
        {
            Spannable wordtoSpan = new SpannableString(word);        
            wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE));// change the color of the word in to blue.
            mainList.set(i, wordtoSpan.toString())//color changed word update to the List.
            break;
        }
    }

    for(int i = 0; i < mainList.size(); i++)
    {
        String word = mainList.get(i);
        tv.append(word);//append each word in List. search word will be blue color
    }


}
相关问题