I fill the ListView from Database. One of the fields in DB contains text with \n. But when I put this text into the item, then I don’t get Newline instead of \n. How does I must correctly take this text from DB and put it into the item?
My code:
txtComment = (TextView) convertView.findViewById(R.id.txt_comment);
this.cur.moveToPosition(position);
txtComment.setText(this.cur.getString(this.cur.getColumnIndex("comment")));
In DB item:
The probe was built to land on the larger … rock to bring back to Earth.\nSuch a venture should …
result in android:
The probe was built to land on the larger … rock to bring back to Earth.\nSuch a venture should …
I expected:
The probe was built to land on the larger … rock to bring back to Earth.
Such a venture should …
TextView from item.xml:
<TextView
android:id="@+id/txt_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:padding="5dip"
android:textColor="#fff"
android:textSize="12sp" />
Content for DB I am taking by parsing JSON. I can ask developer to insert smth else instead of \n. But what?
A string containing a backslash followed by an n is very different to a string containing a newline. It’s only when a string is parsed as a string literal that Java treats them differently. If you want to store a newline in a database, you should just store it in the value to start with. If you want to apply Java string literal escaping, you’ll need to find some appropriate code to do so – which isn’t just as simple as
text.replace("\\n", "\n")for example, as you also need to take account of situations like"escaped\\n".I suspect there’ll be some string utility libraries somewhere which can do this – the
StringEscapeUtilsclass from Apache Commons 3.1 looks like a good starting point, if it works in Android, for example.