Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么';t Html.fromHtml在Android中不适用于字符串?_Java_Android_Firebase - Fatal编程技术网

Java 为什么';t Html.fromHtml在Android中不适用于字符串?

Java 为什么';t Html.fromHtml在Android中不适用于字符串?,java,android,firebase,Java,Android,Firebase,一个字符串值来自我的firebase: if(task.issusccessful()){ //binding.contentMain.noData.setVisibility(View.GONE); for(QueryDocumentSnapshot文档:Objects.requirennull(task.getResult())) postMap.putAll(document.getData()); 试一试{ if(postMap!=null) //binding.desc1.setTex

一个字符串值来自我的firebase:

if(task.issusccessful()){
//binding.contentMain.noData.setVisibility(View.GONE);
for(QueryDocumentSnapshot文档:Objects.requirennull(task.getResult()))
postMap.putAll(document.getData());
试一试{
if(postMap!=null)
//binding.desc1.setText(Objects.requirennoull(postMap.get(CONTENT)).toString();
binding.desc1.setText(Html.fromHtml(Objects.requirennoull(postMap.get(CONTENT)).toString());
}捕获(例外e){
e、 printStackTrace();
}
}
现在,我将告诉您Firestore中字符串的内容,实际上我只是在Firestore集合中手动编写为字符串字段:

This is the first text. \n\n This is a second text. \n This is the third text. \n\n Done. \n
确保上述行仅为一个字符串/firestore字段

在firestore中,我将上述值存储为
字符串
,并获取该值并在Android中显示。但它并没有采取新的路线。相反,它将与其他字符一起显示writed\n

我尝试了使用和不使用HTML。使用\n和\r\n

更新:你们可以看到下图我是如何在Firestore中存储的。

在firestore中,您需要在
n
之前放置两个
\
以添加新行

如果您从用户处获取数据或从文件中读取数据,请在存储数据之前添加此行:

str.replaceAll("\\n", "\n");
使用此方法:

公共跨区getHtmlFormattedString(字符串值){
跨距结果=空;
试一试{
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.N){
结果=Html.fromHtml(值,Html.FROM\u Html\u MODE\u COMPACT);
}否则{
结果=Html.fromHtml(值);
}
}捕获(例外情况除外){
}
返回结果;
}
就你而言:

binding.desc1.setText(getHtmlFormattedString(Objects.requireNonNull(postMap.get(CONTENT)).toString().replaceAll("\\n","<br/>")));
binding.desc1.setText(getHtmlFormattedString(Objects.requirennull(postMap.get(CONTENT)).toString());
更新答案
\n
不是html标记,如果要处理
html。从html
开始,应将
\n
替换为

标记

因此,在你的情况下:

binding.desc1.setText(getHtmlFormattedString(Objects.requireNonNull(postMap.get(CONTENT)).toString().replaceAll("\\n","<br/>")));
binding.desc1.setText(getHtmlFormattedString(Objects.requirennull(postMap.get(CONTENT)).toString().replaceAll(“\\n”,“
”);
您的字符串是

This is the first text. \n\n This is a second text. \n This is the third text. \n\n Done. \n
这很奇怪

1)你想在句号后
下一行吗

2)保存时是否可以编辑或格式化字符串

如果您的案例是
1
,那么您可以做的是

删除所有
/n
字符并替换为

stringstringwithoutnewline=yourString.replaceAll(“\n”和“”)

然后就变成这样了

This is the first text.  This is a second text.  This is the third text.  Done. 
现在删除字符串中的所有“.”,并将其替换为新行

String formattedString=yourString.replaceAll(“.
“,System.lineSeparator())

您可以将这些链接在一行中,因为
replaceAll
返回一个新的
String

你能试试这个吗

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());
            String string = postMap.get(CONTENT).toString();
            String finalString = string.replaceAll("\n","").replaceAll(".  ",System.lineSeparator())
            binding.desc1.setText(finalString)
    } catch (Exception e) {
        e.printStackTrace();
    }
}

发生这种情况是因为存在
\\n
而不是
\n
。您需要将其取消扫描:
s.replaceAll(“\\\\n”,“\\\n”)或者如果您可以只导入ApacheCommons,那么有一个静态方法
String op=StringEscapeUtils.unescapeJava
@AniketSahrawat我需要在Firesotore字段中写什么?请参阅和@AniketSahrawat在我的情况下,它不会替换为\n to\\n;这正是我在“集合”字段中手动编写的内容。@AniketSahrawat检查编辑后的问题。您正在查找单词“unescape”@AniketSahrawat也通过删除空格进行了尝试。不工作它没有将\\n替换为\n。不知道为什么!检查问题中的图像检查问题中的图像无论我在firestore中写了什么,都会发生。看问题,我写了什么。安看到照片里面的问题是接下来会发生什么。我在Firebase有自己的项目。无论我想要什么,我都可以手动在Firesotore DB中编写。我也不想在每一个句号后都换一行。这取决于一些文章。在段落后我想/添加新行,这样,在android中,空间就会自动出现。在这个问题上,我只举了一个例子。@ShefaliSingh我建议您在将字符串保存到firebase之前格式化该字符串,然后您就可以成功格式化该字符串。否则,您将很难格式化字符串。