SQLite snippet function implementation does not format Text as HTML in TextView

情到浓时终转凉″ 提交于 2019-12-20 04:29:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!