换行字符\ñ不TextView的Andr​​oid的正确显示 [英] New Line character \n not displaying properly in textView Android

查看:123
本文介绍了换行字符\ñ不TextView的Andr​​oid的正确显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,如果你做这样的事情

I know that if you do something like

myTextView.setText("This is on first line \n 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:

这是第一行\ n这个是第二行

This is on first line \n This is on second line

下面是code我用它来从数据库中提取的字符串的行:

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

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

我只是填充从一个文本文件,其中每行是一个新进入表,以便行看起来像这样的数据库,这是第一行\ n这个是第二行,它被存储为文本。

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 \n This is on second line" and it is stored as text.

还有一个原因是它没有显示\ n个字符是否正确?它必须是是与在数据库中的字符串之中。有什么建议?

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

推荐答案

由于Falmarri说,在他的评论,当它被放入数据库中的字符串被转义。你可以尝试和反向转义的字符串通过调用的String =反向转义(stringFromDatabase)你把它在你的的TextView 之前。

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的Andr​​oid的正确显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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