[英]SQLite snippet function implementation does not format Text as HTML in TextView
我正在使用SQLite全文搜索實現搜索功能。 我想像Google搜索一樣用粗體查詢文本顯示結果! 我已經實現了類似下面的代碼,但是它通過將視圖綁定到光標適配器並設置TextView
文本格式來顯示純文本,而沒有任何HTML格式 。 我無法弄清楚代碼中的錯誤之處? 任何幫助請!
我在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;
}
在活動類中向用戶顯示結果的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);
文本視圖顯示未格式化的文本,因為代碼告訴它顯示KEY_TEXT_en列的內容,即未格式化的文本。
要顯示摘錄功能的結果,請為其命名:
SELECT ... snippet(...) AS snippet ...
並將該列用於文本視圖。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.