IamAlexAlright IamAlexAlright - 1 year ago 147
Android Question

Removing newline from string

Here's my issue:
I have a database and it is full of episodes of a tv show. One column denotes the episode number. I want to display the episodes in a list like this:

  • Episode 1

  • Episode 2

  • Episode 3

  • etc.

I'm using my own adapter class that extends SimpleCursorAdapter to do this...
Since I had formatting errors I am using

Basically the only reason I have a custom adapter is so I can do something like this:

textView.setText("Episode " + cursor.getString("column_for_episode_number");

The problem is, I get a list that looks like this:

  • Episode

  • 1

  • Episode

  • 2

  • Episode

  • 3

When I try something like this(which worked in a different portion of my code):

String text = "Episode " + cursor.getString("blah");
text = text.replaceAll("\\n","");

I get the exact same list output :(

Why don't I use create a custom view with two textboxes next to each other? It is hard for me to get that to look pretty :/

Answer Source

Check if there is new line at the biggining before you replace and do the same test again

for(int i=0; cursor.getString("blah").length()-1; i++)

   if(cursor.getString("blah").charAt(i)=='\\n')  <-- use the one constant for line separator i fogot what was the name
      Log.i("NEW LINE?", "YES, WE HAVE");


Or use .contains("\n"); method

Check the xml for width of the textview as well.

Why you use getString() when you are fetching integer? Use the getInt() and then Integer.toString(theint) when you are setting in the textview..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download