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

时间:2015-05-28 07:15:15

标签: android listview text highlight android-cursoradapter

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

我有listviewdatabase/cursor获取数据,并在cursoradapter的帮助下显示。我已将searchview放在action bar中以搜索数据库表格中的文本。

现在我希望当我在searchview中输入字符或单词时,database table的每个匹配结果都应突出显示/更改textviewlistview的背景颜色。

我对执行搜索操作的位置(activitycursoradapter)以及如何显示结果感到困惑?

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

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

1 个答案:

答案 0 :(得分:1)

在一段时间后回答道歉,但它仍然可以帮助有需要的人。

我最终要做的是将文本传递给适配器中的方法,然后使用bindView()Pattern-Matcher中找到文本并使用SpannableString突出显示文本

以下是适配器中的代码:

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