【发布时间】:2020-09-20 11:40:11
【问题描述】:
一个字符串值来自我的 firebase:
if (task.isSuccessful()) {
// binding.contentMain.noData.setVisibility(View.GONE);
for (QueryDocumentSnapshot document : Objects.requireNonNull(task.getResult()))
postMap.putAll(document.getData());
try {
if (postMap != null)
//binding.desc1.setText(Objects.requireNonNull(postMap.get(CONTENT)).toString());
binding.desc1.setText(Html.fromHtml(Objects.requireNonNull(postMap.get(CONTENT)).toString()));
} catch (Exception e) {
e.printStackTrace();
}
}
现在,我将告诉你 Firestore 中的 String 里面有什么,实际上我只是在 Firestore 集合中手动编写为 String 字段。:
This is the first text. \n\n This is a second text. \n This is the third text. \n\n Done. \n
确保上面一行只是一个字符串/firestore 字段。
在 Firestore 中,我将上述值存储为 String,然后获取该值并在 Android 中显示它。但它并没有采取新的路线。相反,它会显示书写的 \n 以及其他字符。
我尝试过使用和不使用 HTML。与 \n 和 \r\n。
更新:您可以在下图看到我在 Firestore 中的存储方式。 https://drive.google.com/open?id=19yry9top_W8LREw5SEaZKWYWSlnCP1io
【问题讨论】:
-
这是因为
\\n而不是\n。你需要取消它:s.replaceAll("\\\\n", "\\\n");或者如果你可以导入 apache commons 那么有一个静态方法String op = StringEscapeUtils.unescapeJava(s); -
@AniketSahrawat 我到底需要在 Firesotore 字段中写些什么?
-
@AniketSahrawat 在我的情况下,它不会用 \n 替换为 \\n ;它完全符合我在收藏字段中手动编写的内容。
-
@AniketSahrawat 检查已编辑的问题。