【问题标题】:Strike through text is not saved to SQLite database删除文本不会保存到 SQLite 数据库
【发布时间】:2018-12-07 15:10:07
【问题描述】:

我遇到的问题是,使用 StrikethroughSpan 时,删除线文本没有保存到我的应用程序的 SQLite 数据库中。但它适用于其他跨度,特别是 StyleSpan 和 UnderlineSpan - 粗体/斜体/下划线文本正确保存到数据库并由 CursorAdapter 显示。我为此目的使用 HtmlCompat。但是删除线显示为没有格式的纯文本。

我对此事的网络研究没有给出任何结果。这种行为的原因是什么,有没有办法解决这个问题?

我将跨区文本保存到数据库的代码:

ContentValues values = new ContentValues();
        NoteCursorAdapter cursorAdapter = new NoteCursorAdapter(this, null);

        String newHtmlString = HtmlCompat.toHtml(noteText, TO_HTML_PARAGRAPH_LINES_CONSECUTIVE);

        values.put(NoteEntry.COLUMN_NOTE_TEXT, newHtmlString);

活动中:

@Override
public void bindView(View view, Context context, Cursor cursor) {
            ...
        String htmlFormString = cursor.getString(noteBitmapColumnIndex);
        Spanned spannedText = HtmlCompat.fromHtml(htmlFormString, FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE);
        noteBitmap.setText(spannedText);
}

在 CursorAdapter 中:

@Override
public void onLoadFinished(...) {
    ...

    htmlString = cursor.getString(textColumnIndex);
    realText = HtmlCompat.fromHtml(htmlFormString, FROM_HTML_SEPARATOR_LINE_BREAK_BLOCKQUOTE);    
    mNoteText.setText(realText);

放入 ContentValues (newHtmlString) 的 html 字符串显示相应的 HTML 标签:

I/System.out: <p dir="ltr"><b><b><strike>Word</strike></b></b></p>

但格式不显示。

【问题讨论】:

  • 您如何存储数据?从数据库中读取的 html 字符串是什么样的?
  • 请看我的更新。我使用 DBHelper/ContentProvider 存储数据并通过 HtmlCompat.toHtml 转换文本。放在 ContentValues (newHtmlString) 中的 html 字符串显示标签 : I/System.out:

    Word

标签: android html sqlite save strikethrough


【解决方案1】:

Html 类在它支持的标签方面没有很好的记录。从 Android 5 和 Android 9 检查了它的源代码。Android 9 版本支持两种方式的删除线(ref),而 Android 5 只输出它,但在fromHtml()ref)中不读取它。没有仔细研究它是什么时候改变的。

您可以通过向fromHtml 提供自定义标记处理程序来解决此问题。这是一个例子:Android: How to use the Html.TagHandler?

【讨论】:

  • 我明白了。非常感谢您的帮助!我会这样尝试。
猜你喜欢
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
相关资源
最近更新 更多