SQLite的片段功能的实现不设置文本格式为HTML中的TextView [英] SQLite snippet function implementation does not format Text as HTML in TextView
问题描述
我使用的SQLite的全文搜索实现搜索功能。我想present结果与大胆的查询文本作为谷歌搜索呢!我已经实现了code类似下面,但它的的没有任何HTML显示了纯文本格式的虽然结合以光标适配器和设置的文本格式的TextView
。我想不通,我是错的,在code?任何帮助,请!
我的搜索在DatabaseAdapter类的功能是:
公开光标searchText(字符串的inputText)抛出的SQLException
{
Log.w(TAG,inputText的);
查询字符串=SELECT+
文档ID为_id,+
KEY_NAME +,+
KEY_INDEX +,+
KEY_TEXT_en +,+
KEY_TEXT_ur +,+
KEY_TRANS_ur +,+
六角(matchinfo(+ FTS_VIRTUAL_TABLE +))为+ KEY_OCCURRENCES +,+
片断(+ FTS_VIRTUAL_TABLE +,&其中b取代;','&所述; / B个')+
从+ FTS_VIRTUAL_TABLE +
在这里(+ FTS_VIRTUAL_TABLE +)匹配'+\+*+的inputText +\*+;;
Log.w(TAG,查询);
光标mCursor = mDb.rawQuery(查询,NULL);
如果(mCursor!= NULL)
{
mCursor.moveToFirst();
}
返回mCursor;
}
和我的结果显示函数来显示结果用户在活动课是:
公共无效showResults(查询字符串)
{
光标光标= mDbHelper.searchText((查询= NULL query.toString():@@@@)!?);
如果(光标== NULL)
{
}
其他
{
//指定列,我们希望的结果显示
的String []从=新的String []
{
DbAdapter.KEY_TEXT_en,
DbAdapter.KEY_INDEX,
DbAdapter.KEY_NAME,
};
//指定我们想要的列去相应的布局元素
INT []到=新INT []
{
R.id.TextEnTv,
R.id.IndexTv,
R.id.NameTv
};
最后SimpleCursorAdapter的CursorAdapter =新simpleCursorAdapter(这一点,R.layout.search_results,光标,从,到);
cursorAdapter.setViewBinder(新SimpleCursorAdapter.ViewBinder(){
公共布尔setViewValue(查看视图,光标指针,整数参数:columnIndex){
如果(参数: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))));
返回(真);
}
返回(假);
}
});
mListView.setAdapter(CursorAdapter的);
文本视图显示未格式化的文本,因为code告诉它,以显示KEY_TEXT_en列,这是无格式文本的内容。
要显示的代码片段函数的结果,给它一个名字:
SELECT ...片段(...)AS片段...
和使用该列文本视图。
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);
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.
这篇关于SQLite的片段功能的实现不设置文本格式为HTML中的TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!