如何更改搜索文字的背景色在列表视图,CursorAdapter的,而在搜索查看打字? [英] How to change background color of searched text in listview-cursoradapter while typing in searchview?

查看:203
本文介绍了如何更改搜索文字的背景色在列表视图,CursorAdapter的,而在搜索查看打字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过关于列表视图使用 Spannable 字符串高亮显示文本教程,但有没有来自文本搜索数据库和突出列表视图

我有一个列表视图数据库/光标获取数据,并与<$ C $的帮助显示C>的CursorAdapter 。我已经放在搜索查看操作栏来从数据库表中搜索文本。

现在我想,当我键入搜索查看一个字符或单词,从数据库表每个匹配的结果应该突出 TextView中的/更改背景颜色列表视图

我搞不清在哪里执行搜索操作(活动的CursorAdapter ),以及如何显示结果?

这code在活动,我可以从数据库中包含查询结果得到

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单){    。getMenuInflater()膨胀(R.menu.search_menu,菜单);    SearchManager的SearchManager的=(SearchManager的)getSystemService(Context.SEARCH_SERVICE);
    搜索查看搜索查看=(搜索查看)menu.findItem(R.id.action_search)
            .getActionView();
    sea​​rchView.setSearchableInfo(SearchManager的
            .getSearchableInfo(getComponentName()));
    sea​​rchView.setOnQueryTextListener(本);
    返回true;
}@覆盖
公共布尔onQueryTextChange(字符串文本){    SEARCHTEXT(文字+*);
    返回false;
}@覆盖
公共布尔onQueryTextSubmit(字符串文本){    SEARCHTEXT(文字+*);
    返回false;
}私人无效SEARCHTEXT(字符串文本){    如果(文字!= NULL){        localDB.openDB();
        光标光标= localDB.searchDBText(文本);
        如果(光标=空&放大器;!&放大器; cursor.moveToFirst()){            字符串消息= cursor.getString(光标
                    .getColumnIndex(LocalStoreDB.ROW_MESSAGE));            Log.i(SEARCHTEXT消息:,邮件);
        }其他{            Log.i(SEARCHTEXT消息:,光标是空);
        }
        cursor.close();
        localDB.closeDB();
    }其他{        Log.i(SEARCHTEXT消息:,输入文本为空);
    }
}


解决方案

道歉了一会儿后回答,但还是会帮助别人需要的人。

我最终什么事做的是通过文本适配器的方法,那么找到文本bindView()模式匹配器,并强调与 SpannableString

以下是适配器code:

 公共类AdapterSingleChat扩展的CursorAdapter {    私有静态诠释indexThumb;    //要搜索的字符串和突出
    字符串textToSearch = NULL;    公共AdapterSingleChat(上下文的背景下,光标光标,INT标志){
        超(上下文,光标,标志);        //缓存列索引
        indexThumb = cursor.getColumnIndex(SQLiteStoreDB.ROW_TEXT);
   }   // ViewHolder()...   // NewView的()......   @覆盖
   公共无效bindView(查看视图,上下文的背景下,光标光标){        //从游标文本中搜索将执行
        字符串cursorText = cursor.getString(indexText);        // Spannable字符串匹配突出词搜索
        SpannableString spannableStringSearch = NULL;        如果((textToSearch = NULL)及!&安培;!(textToSearch.isEmpty())){
            spannableStringSearch =新SpannableString(cursorText);            //编译输入文本的模式
            模式模式= Pattern.compile(textToSearch,
                    Pattern.CASE_INSENSITIVE);            //给compliled模式匹配器发现光标文字匹配模式
            匹配匹配= pattern.matcher(cursorText);
            spannableStringSearch.setSpan(新BackgroundColorSpan(
                        Color.TRANSPARENT),0,spannableStringSearch.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            而(matcher.find()){                //突出光标所有匹配的单词,白色背景(因为我有一个创辉背景图片)
                spannableStringSearch.setSpan(新BackgroundColorSpan(
                            Color.WHITE),matcher.start(),matcher.end(),
                    Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }        如果(spannableStringSearch!= NULL {            //如果搜索已经进行设置的TextView spannable串
            holder.tvCursorText.setText(spannableStringSearch);
        }其他{            //否则设置纯文本光标
            holder.tvCursorText.setText(cursorText);
        }
   }   //把文本从活动(从SearchManager的,EditText上或任何其他输入型)在这里
   私人无效SEARCHTEXT(字符串文本){       this.textToSearch =文本;
   }

是的,不要忘了 swapCursor() notifyDataSetChanged()后输入文字。

I've seen tutorials about highlighting text in listview using Spannable string but there is none about searching text from database and highlighting in listview.

I have a listview which gets data from database/cursor and shows with the help of cursoradapter. I've placed searchview in action bar to search text from database table.

Now i want that when i type a character or word in searchview, every matched result from database table should highlight/change background color of textview in listview.

I'm confuse about where to perform search operation(in activity or cursoradapter) and how to display result ?

This code is in activity and i can get result from db with like query.

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

解决方案

Apologize for answering after a while, but still it would help someone in need.

What i end up doing is pass the text to a method in adapter, then find the text in bindView() with Pattern-Matcher and highlight the text with SpannableString

Following is the code in adapter:

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;
   }

and yes, do not forget to swapCursor() or notifyDataSetChanged() after inputting words.

这篇关于如何更改搜索文字的背景色在列表视图,CursorAdapter的,而在搜索查看打字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆