Search code examples
androidhtmlsqlitesearchtrim

Strip html from sqlite search on android


I have a data, let's say this is my data:

<div class=main>
<ol>

  <li>Vestibulum feugiat nisi dolor, et pharetra nisl tincidunt et. <span class=b1>Pellentesque</span> metus sem, condimentum quis massa nec, fringilla consectetur risus. </li>
  <li>Morbi eu nisi quis dui ultricies luctus eu facilisis diam. <span class=b1>Pellentesque</span> non orci lorem. Suspendisse elementum rhoncus nulla sit amet condimentum. </li>
</ol>
</div>

That data is stored in sqlite in table "ini" and column "Text".

And I have this search function:

public void search(View view) {

    String cari = searchText.getText().toString();

    if(cari.length() > 3){
    // || is the concatenation operation in SQLite
    cursor = db.rawQuery("SELECT Text FROM ini WHERE Text LIKE ?", 
                    new String[]{"%" + cari + "%"});
    adapter = new SimpleCursorAdapter(
            this, 
            R.layout.search_list_item, 
            cursor, 
            new String[] {"Text"}, 
            new int[] {R.id.text});
    setListAdapter(adapter);
    }else{
        setListAdapter(null);
    }   
}

Let's say that I want to search "Pellentesque metus", which has </span> between two words (Pellentesque </span> metus). how to remove this </span>? or how to trim all html when the search query called?

Sorry for my english

Thanks in advance


Solution

  • You can create additional column in your table and put stripped html

    html.replaceAll("<[^>]*>", StringUtil.EMPTY)
    

    Then you can use this column insteadof your real column.