换行符 在 textView Android 中无法正确显示 [英] New Line character not displaying properly in textView Android

查看:40
本文介绍了换行符 在 textView Android 中无法正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如果你做类似的事情

I know that if you do something like

myTextView.setText("This is on first line 
 This is on second line");

然后它会像这样正确显示:

Then it will display properly like this:

这是在第一行
这是在第二行

This is on first line
This is on second line

当我将该字符串存储在数据库中,然后将其设置为显示的视图时:

When I store that string in a database and then set it to the view it displays as such:

第一行 第二行

这是我用来从数据库中提取字符串的代码行:

Here is the line of code I use to extract the string from the database:

factView.setText(factsCursor.getString(MyDBAdapter.FACT_COLUMN));

我只是从一个文本文件中填充数据库,其中每一行都是表中的一个新条目,所以一行看起来像这样这是在第一行 这是在第二行",它被存储为文本.

I simply populate the database from a text file where each line is a new entry into the table so a line would look like this "This is on first line This is on second line" and it is stored as text.

是否有原因没有正确显示 字符?它必须与数据库中的字符串有关.有什么建议吗?

Is there a reason that it isn't displaying the characters properly? It must be something to do with the string being in the database. Any suggestions?

推荐答案

正如 Falmarri 在他的评论中所说,您的字符串在放入数据库时​​会被转义.在将字符串放入 TextView 之前,您可以尝试通过调用 String s = unescape(stringFromDatabase) 来取消转义字符串.

As Falmarri said in his comment, your string is being escaped when it is put into the database. You could try and unescape the string by calling String s = unescape(stringFromDatabase) before you place it in your TextView.

附带说明,在将数据插入数据库时​​,请确保对来自用户或未知可变源的任何类型的数据使用 DatabaseUtils.sqlEscapeString().这将保护您免受错误和 SQL 注入的影响.

As a side note, make sure you are using DatabaseUtils.sqlEscapeString() on any kind of data that is from the user or an unknown changeable source when inserting data into the database. This will protect you from errors and SQL Injection.

这篇关于换行符 在 textView Android 中无法正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆