I have an SQLite database of items where some of them contain basic HTML such as b and sub tags. I've bound the table to a ListView using a SimpleCursorAdapter. Is there a way to make the ListView format the HTML tags so it displays properly? It seems like the way forward is to get the Cursor to deliver SpannedStri开发者_StackOverflow社区ngs but I can't work out how to do that.
Thanks Ian, this is my final adapter code:
private class HtmlCursorAdapter extends SimpleCursorAdapter {
public HtmlCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
}
@Override
public void setViewText (TextView view, String text) {
view.setText(Html.fromHtml(text),BufferType.SPANNABLE);
}
}
If your data contains only simple HTML tags, they can actually be handled by a TextView by using Html.fromHtml(yourString). That static method returns a Spanned, which can be displayed by a TextView with far less overhead than a WebView.
You could use the CursorWrapper class.
CursorWrapper cw = new CursorWrapper(myCursor) {
public String getString(int columnIndex) {
String withHTML = super.getString(columnIndex);
return Html.fromHtml(withHTML).toString();
}
};
精彩评论