开发者

Android: Display HTML formatted text in a ListView when loaded from database

开发者 https://www.devze.com 2023-02-04 14:13 出处:网络
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 fo

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();
    }
};
0

精彩评论

暂无评论...
验证码 换一张
取 消