我已经看过关于如何在listview中使用Spannable字符串突出显示文本的教程,但是没有关于从数据库中搜索文本和在listview中突出显示文本的教程。
我有一个listview,它从database/cursor获取数据,并在cursoradapter的帮助下显示。我将searchview放在action bar中,以便从数据库表中搜索文本。
现在,我希望在搜索视图中键入字符或单词时,database table的每个匹配结果都应该突出显示/更改textview在listview中的背景色。
我对在哪里执行搜索操作(在activity或cursoradapter中)以及如何显示结果感到困惑。
这段代码处于活动状态,我可以使用类似的查询从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");
}
}发布于 2015-12-15 10:41:45
抱歉,过了一会儿再回答,但还是会帮助到需要帮助的人。
最后我要做的是将文本传递给适配器中的一个方法,然后用Pattern-Matcher在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()。
https://stackoverflow.com/questions/30499649
复制相似问题