SQLite 片段函数实现不会在 TextView 中将文本格式化为 HTML
Posted
技术标签:
【中文标题】SQLite 片段函数实现不会在 TextView 中将文本格式化为 HTML【英文标题】:SQLite snippet function implementation does not format Text as HTML in TextView 【发布时间】:2014-10-25 17:31:30 【问题描述】:我正在使用 SQLite FULL TEXT SEARCH 实现搜索功能。我想像 Google 搜索一样用粗体查询文本呈现结果!我已经实现了类似下面的代码,但它显示没有任何 html 格式的纯文本,尽管将视图绑定到光标适配器并设置 TextView
的文本格式。我无法弄清楚我在代码中哪里错了?请帮忙!
我在 DatabaseAdapter 类中的搜索功能是:
public Cursor searchText(String inputText) throws SQLException
Log.w(TAG, inputText);
String query = "SELECT "+
"docid as _id," +
KEY_NAME + "," +
KEY_INDEX + "," +
KEY_TEXT_en + "," +
KEY_TEXT_ur + "," +
KEY_TRANS_ur + "," +
"hex(matchinfo("+FTS_VIRTUAL_TABLE+")) AS "+KEY_OCCURRENCES+"," +
"snippet("+FTS_VIRTUAL_TABLE+",'<b>','</b>')" +
" from " + FTS_VIRTUAL_TABLE +
" where ("+ FTS_VIRTUAL_TABLE +") MATCH '" + "\""+"*"+inputText+"\"*"+" ';";
Log.w(TAG, query);
Cursor mCursor = mDb.rawQuery(query,null);
if (mCursor != null)
mCursor.moveToFirst();
return mCursor;
而我在 Activity 类中向用户显示结果的 show Results 函数是:
public void showResults(String query)
Cursor cursor = mDbHelper.searchText((query != null ? query.toString() : "@@@@"));
if (cursor == null)
else
// Specify the columns we want to display in the result
String[] from = new String[]
DbAdapter.KEY_TEXT_en,
DbAdapter.KEY_INDEX,
DbAdapter.KEY_NAME,
;
// Specify the Corresponding layout elements where we want the columns to go
int[] to = new int[]
R.id.TextEnTv,
R.id.IndexTv,
R.id.NameTv
;
final SimpleCursorAdapter cursorAdapter = new simpleCursorAdapter(this,R.layout.search_results, cursor, from, to);
cursorAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder()
public boolean setViewValue(View view,Cursor cursor, int columnIndex)
if (columnIndex == cursor.getColumnIndex(DbAdapter.KEY_TEXT_en))
TextView myView = (TextView) findViewById(R.id.TextEnTv);
myView.setText(Html.fromHtml(cursor.getString(cursor.getColumnIndex(DbAdapter.KEY_TEXT_en))));
return(true);
return(false);
);
mListView.setAdapter(cursorAdapter);
【问题讨论】:
【参考方案1】:文本视图显示无格式文本,因为代码告诉它显示 KEY_TEXT_en 列的内容,即无格式文本。
要显示sn-p函数的结果,给它一个名字:
SELECT ... snippet(...) AS snippet ...
并将该列用于文本视图。
【讨论】:
以上是关于SQLite 片段函数实现不会在 TextView 中将文本格式化为 HTML的主要内容,如果未能解决你的问题,请参考以下文章