问题
I am implementing a search function using SQLite FULL TEXT SEARCH. I want to present the results with bold query text as Google search does! I have implemented code something like below but it displays the plain text without any HTML formatting though binding view to the cursor adapter and setting text formatting of the TextView
. I cannot figure out where I am wrong in the code? Any Help Please!
My search function in DatabaseAdapter Class is:
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;
}
And my show Results function to show results to users in the Activity Class is:
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:
The text view displays unformatted text because the code tells it to show the contents of the KEY_TEXT_en column, which is unformatted text.
To display the result of the snippet function, give it a name:
SELECT ... snippet(...) AS snippet ...
and use that column for the text view.
来源:https://stackoverflow.com/questions/26565218/sqlite-snippet-function-implementation-does-not-format-text-as-html-in-textview