在 searchview 中键入时,如何更改 listview-cursoradapter 中搜索文本的背景颜色?

How to change background color of searched text in listview-cursoradapter while typing in searchview?

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

我有一个 listview,它从 database/cursor 获取数据并在 cursoradapter 的帮助下显示。我已将 searchview 放入 action bar 以从数据库 table.

中搜索文本

现在我希望当我在搜索视图中键入字符或单词时,来自 database table 的每个匹配结果都应该 highlight/change listviewtextview 的背景色。

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

此代码在 activity 中,我可以使用类似查询从数据库中获取结果。

@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");
    }
}

抱歉过了一会儿才回答,但它仍然可以帮助有需要的人。

我最后做的是将文本传递给适配器中的方法,然后用 Pattern-MatcherbindView() 中找到文本并用 SpannableString[=16= 突出显示文本]

适配器中的代码如下:

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()