首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在输入搜索视图时更改listview-cursoradapter中搜索文本的背景色?

如何在输入搜索视图时更改listview-cursoradapter中搜索文本的背景色?
EN

Stack Overflow用户
提问于 2015-05-28 07:15:14
回答 1查看 1.2K关注 0票数 3

我已经看过关于如何在listview中使用Spannable字符串突出显示文本的教程,但是没有关于从数据库中搜索文本和在listview中突出显示文本的教程。

我有一个listview,它从database/cursor获取数据,并在cursoradapter的帮助下显示。我将searchview放在action bar中,以便从数据库表中搜索文本。

现在,我希望在搜索视图中键入字符或单词时,database table的每个匹配结果都应该突出显示/更改textviewlistview中的背景色。

我对在哪里执行搜索操作(在activitycursoradapter中)以及如何显示结果感到困惑。

这段代码处于活动状态,我可以使用类似的查询从db获得结果。

代码语言:javascript
复制
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.search_menu, menu);

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_search)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setOnQueryTextListener(this);
    return true;
}

@Override
public boolean onQueryTextChange(String text) {

    searchText(text + "*");
    return false;
}

@Override
public boolean onQueryTextSubmit(String text) {

    searchText(text + "*");
    return false;
}

private void searchText(String text) {

    if (text != null) {

        localDB.openDB();
        Cursor cursor = localDB.searchDBText(text);
        if (cursor != null && cursor.moveToFirst()) {

            String message = cursor.getString(cursor
                    .getColumnIndex(LocalStoreDB.ROW_MESSAGE));

            Log.i("searchText message:", message);
        } else {

            Log.i("searchText message:", "cursor is null");
        }
        cursor.close();
        localDB.closeDB();
    } else {

        Log.i("searchText message:", "input text is null");
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-12-15 10:41:45

抱歉,过了一会儿再回答,但还是会帮助到需要帮助的人。

最后我要做的是将文本传递给适配器中的一个方法,然后用Pattern-MatcherPattern-Matcher中找到文本,然后用SpannableString突出显示文本。

以下是适配器中的代码:

代码语言:javascript
复制
public class AdapterSingleChat extends CursorAdapter {

    private static int indexThumb;

    //String to search and highlight
    String textToSearch = null;

    public AdapterSingleChat(Context context, Cursor cursor, int flags) {
        super(context, cursor, flags);

        //caching the column index
        indexThumb = cursor.getColumnIndex(SQLiteStoreDB.ROW_TEXT);
   }

   //ViewHolder()...

   //newView()....

   @Override
   public void bindView(View view, Context context, Cursor cursor){

        //Text from cursor in which search will perform
        String cursorText = cursor.getString(indexText);

        //Spannable string to highlight matching searched words
        SpannableString spannableStringSearch = null;

        if ((textToSearch != null) && (!textToSearch.isEmpty())) {


            spannableStringSearch = new SpannableString(cursorText);

            //compile the pattern of input text
            Pattern pattern = Pattern.compile(textToSearch,
                    Pattern.CASE_INSENSITIVE);

            //giving the compliled pattern to matcher to find matching pattern in cursor text
            Matcher matcher = pattern.matcher(cursorText);
            spannableStringSearch.setSpan(new BackgroundColorSpan(
                        Color.TRANSPARENT), 0, spannableStringSearch.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            while (matcher.find()) {

                //highlight all matching words in cursor with white background(since i have a colorfull background image)
                spannableStringSearch.setSpan(new BackgroundColorSpan(
                            Color.WHITE), matcher.start(), matcher.end(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        if(spannableStringSearch != null{

            //if search has performed set spannable string in textview
            holder.tvCursorText.setText(spannableStringSearch);
        }else{

            //else set plain cursor text
            holder.tvCursorText.setText(cursorText);
        }
   }

   //Pass the text from activity(from  SearchManager, EditText or any other input type) here
   private void searchText(String text) {

       this.textToSearch = text;
   }

是的,输入单词后,不要忘记swapCursor()notifyDataSetChanged()

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

https://stackoverflow.com/questions/30499649

复制
相关文章

相似问题

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