views:

184

answers:

3

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:

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

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

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?

+1  A: 

Try \\n instead of \n. If it throws an exception than use newline keyword in place of \n....newline is one character, ascii 10; it's often entered in a string literal...and will serve your purpose....:)

Rohan K
I am assuming you meant to surround the \n with white space. That didn't work. I'm a little confused on what you mean by use the new line keyword. There was no exception thrown.
Mike
I've edited this post to make it clear what Rohan meant.
MatrixFrog
+1  A: 

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.

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.

Austyn Mahoney
A: 

I still have the same problem even i used URLDecoder.decode(myString) to unescape '\' caractere but in the interface i get \n not the new ligne, im providing data from the net with json and utf8 encoding also i used Log.d to look the the string . any suggestions ?

sami boussacsou