从API调用设置新行不会创建换行。

huangapple go评论71阅读模式
英文:

Setting a new line from API call not creating newline

问题

设置新行而不进行网络调用对我有效:

mTextView.setText("Message 1 \nMessage 2");

[![enter image description here][1]][1]

但是当我从API调用中的JSON设置文本时,它对我不起作用:

mTextView.setText(userInfo.getDescription());

[![enter image description here][2]][2]

它在我的数据库中存储的格式是:hello \nTest message.

问题是什么?

编辑:

尝试这样做,但也不起作用

String x = description.replace("\n", System.lineSeparator());
Log.d(TAG, "formatDescription: Formatted: " + x);
return x;```

[![enter image description here][3]][3]


  [1]: https://i.stack.imgur.com/5GDhz.png
  [2]: https://i.stack.imgur.com/C2FSc.png
  [3]: https://i.stack.imgur.com/F1Vcg.png

编辑2:

需要在``` .replace("\n", System.lineSeparator());``` 中添加额外的```"\"```才能使其起作用

<details>
<summary>英文:</summary>

Setting a new line without a network call works for me:

```mTextView.setText(&quot;Message 1 \nMessage 2&quot;);```

[![enter image description here][1]][1]


However when I set the text from the JSON in an API call it doesn&#39;t work for me:

```mTextView.setText(userInfo.getDescription());```

[![enter image description here][2]][2]

The format it&#39;s stored in my db is: ```hello \nTest message.```


What is the issue?

Edit: 

Tried doing this, but doesn&#39;t work either

Log.d(TAG, "formatDescription: Description: " + description);
String x = description.replace("\n", System.lineSeparator());
Log.d(TAG, "formatDescription: Formatted: " + x);
return x;


[![enter image description here][3]][3]


  [1]: https://i.stack.imgur.com/5GDhz.png
  [2]: https://i.stack.imgur.com/C2FSc.png
  [3]: https://i.stack.imgur.com/F1Vcg.png

Edit 2: 

Need to add an extra ```&quot;\&quot;``` to ```.replace(&quot;\n&quot;, System.lineSeparator());``` for it to work

</details>


# 答案1
**得分**: 2

String description = userInfo.getDescription();
description = description.replace("\\n", System.lineSeparator());
System.out.println(description);
mTextView.setText(description);

<details>
<summary>英文:</summary>

    String description = userInfo.getDescription();
    description = description.replace(&quot;\\n&quot;, System.lineSeparator());
    System.out.println(description);
    mTextView.setText(description);

</details>



# 答案2
**得分**: 0

String desc = userInfo.getDescription().replaceAll("\\n", System.getProperty("line.separator"));

<details>
<summary>英文:</summary>

Try this:

    String desc = userInfo.getDescription().replaceAll(&quot;\\n&quot;, System.getProperty(&quot;line.separator&quot;));

</details>



huangapple
  • 本文由 发表于 2020年1月3日 17:36:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定