Removing newline from string - android

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 Android.R.layout.simple_list_item_1 and Android.R.id.text1
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 :/

text.replaceAll(System.getProperty("line.separator"), "");

There is a mistake in your code. Use "\n" instead of "\\n"
String myString = "a string\n with new line"
myString = myString.replaceAll("\n","");
Log.d("myString",myString);

Check if there is new line at the beginning 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 constant for the line separator
{
Log.i("NEW LINE?", "YES, WE HAVE");
}
}
Or use the .contains("\n"); method:
Check the xml for the width of the textview as well.
Why are you using getString() when you are fetching an integer? Use getInt() and then use Integer.toString(theint) when you are setting the values in a textview.

This could help you:
response = response.replaceAll("\\s+","");

It sounds like you are hitting wrapping issues rather than newline issues. Change this:
String text = "Episode " + cursor.getString("blah");
To this:
String text = "Episode" + cursor.getString("blah");
And see if that changes the output. Post your layout xml please?

this worked for my (on android 4.4):
(where body is a string with a newline entered from an EditText view on handset)
for (int i=0; i<body.length(); i++) {
if (body.charAt(i) == '\n' || body.charAt(i) == '\t') {
body = body.substring(0, i) + " " + body.substring(i+1, body.length());
}
}

have you tried
cursor.getString("blah").trim()

Related

Kotlin "Do not concatenate text displayed with setText. Use resource string with placeholders. "

I want to set texts: Station 1, Station 2, Station 3...
If I have code like this (frame is of type ScrollView) I get the warning: "Do not concatenate text displayed with setText. Use resource string with placeholders."
for(i in 1..10){
frame.stationTextView.text = "Station $i"
}
If, however I do this, I don't get the warning.
for(i in 1..10){
val str = "Station $i"
frame.stationTextView.text = str
}
The second example seems redundant to me, but the warning is gone. Which one is correct, probably neither. I'm not sure how to use resource string with index that is changing.
Create a string resource like following
<string name="station_counter_text">Station %1$d</string>
Then from Activity/Fragment use like following
for(i in 1..10)
frame.stationTextView.text = getString(R.string. station_counter_text,i)

Android java compare xml string with edittext

I want to compare a xml string with a string from an edittext oder button.
first I set the text of the button:
button1.setText(getString(R.string.okey));
and now I want to check if the text from the button is the same as R.string.okey from the xml file. Like this I can leave out a new variable.
Is it possible to check if the strings are the same with something like this?
if (button1.getText().toString().equals(getString(R.string.okey))){
}
But that doesn't work for me.
Thank you in advance.
this must work, its just to simple. you must change somehow text on button or maybe getString returns different text (Locale changed?). use logging or debugger to check what is button1.getText().toString() and getString(R.string.okey) at the moment of comparison (equals call)
boolean areEqual = button1.getText().toString().equals(getString(R.string.okey));
Log.i("justChecking", "getString:" + getString(R.string.okey) +
", button1.getText:" + button1.getText().toString() +
", are equal:" + areEqual);
if (areEqual){
}
How to Write and View Logs with Logcat
Store them in variables
String a = button1.getText()+"";
String b = getString(R.string.str)+"";
if(a.equals(b)){ }

Arraylist - how to?

So I have this array List:
private ArrayList<String> output;
Which look like this normally once items are added looks like this:
["ready", "30 min", "5.2 mi", "stop"]
But sometimes the order can change on how the information comes in for example:
["30 min", "stop", "5.2 mi", "ready"]
What would be the best solution to get the number that is in front of mi and min since the ArrayList can change I can't use list.get(1) for example cause the index might be something different.
I have tried to use list.getIndexOf("mi"); but that didn't work, kept coming back as -1 which I would think cause it wasn't an exact match.
Use this .. to get no before "mi"
for(int i =0; i<list.size();i++)
{
if(list.get(i).contains("mi"))
{
String[] splitT =
list.get(i).split(" ");
System.out
.println("before mi is" + splitT[0]);}
}
Note splitT[0] is the answer what you want.. like this you can find for "min" too..
If I understood your question correctly, you want to get an element whith a certain pattern to further work with that information. In that case you could loop through the ArrayList and check for element whether or not it matches a certain regexp. Something like this:
for (String s : arraylist) {
if (s.matches("[0-9]+ mi(n)?")) //would match any number, followed by a whitespace and mi/min {
//do something
}
}
You can do something likes this.
for (String item:list){
if (item.contains("mi")&&!item.contains("min")){
Log.d("item",item.substring(0,3));
}
}
You can do similarly for "min".
You can use this code:
for (int i=0 ; i<output.size() ; i++){
if (output.get(i).endsWith("mi")) // index i contains "mi"
else if (output.get(i).endsWith("min")) //index i contains min not mi
}

set textview's text using Html.fromHtml inside a loop?

Is possible to use Html.fromHtml to the String fetchData, to change some of the text style? This text assigned to a textView outside the loop.
Here is the related code:
if( c != null && c.moveToFirst() ){
while (c.isAfterLast()==false) {
String gtWord = c.getString(1);
String gtDef = c.getString(2);
fetchData = fetchData + getResources().getString(R.string.wordLabel) + gtWord + "\n"
+ getResources().getString(R.string.transLabel) + gtDef + "\n\n";
c.moveToNext();
}
getData.setText(fetchData)
Right now I'm using strings.xml in which I've set the text like this:
<b>Word: </b>
but the style is ignored. I'found some related questions and I've tried to do it without using strings.xml also, but the only tag recognized is the , all others ignored. I'm supposing that the problem is that I'm using mixed variables and hardcoded text inside loop, because I tested it outside the loop like this:
getData.setText(Html.fromHtml("<b>This<b/> is <u>underlined<u/> text")
and it's working.
Declaire your string like this
<string name="wordLabel"><![CDATA[<b>Word: </b>]]></string>
From documentaion you can also write it as
<string name="word_label"><b>Word: </b></string> // here I have chnaged your string name wordLabel to word_label. you should follow naming convension.
Notice that the opening bracket is HTML-escaped, using the < notation.

Parse a string and get certain values

I have a string like this:
_id:2 thread_id:189 address:0292 m_size:null person:0 date:1372494272447 date_sent:0 protocol:0 read:1 status:-1 type:1 reply_path_present:0 subject:null body:Ok. Reply message. service_center:051108 locked:0 sim_id:0 error_code:0 seen:1 _id:1 thread_id:189 address:292 m_size:null person:0 date:1372493695831 date_sent:0 protocol:null read:1 status:-1 type:2 reply_path_present:null subject:null body:Test message service_center:null locked:0 sim_id:0 error_code:0 seen:0
I want to retrieve only parts of this string, for example the address:0292 and the body:xyz from the entire string. I want all instances of these two from a very large String (above is just a sample). Let's assume its more than 20000 characters.
How can I achieve this?
Looks like every address is followed by m_size, so use the string.split() function, to split over the keyword address then select the string.substring() (from each string in the resulting array) until reaching the keyword m_size. And repeat the entire thing for the keywords body and service_center. I can't think of any other way.
You are right, it doesn't seem pretty. But it works :)
String[] splitString = string.split(" ");
for (int i = 0; i < splitString.length; i++) {
if (splitString[i].startsWith("body") || splitString[i].startsWith("address"))
Log.i(TAG, "Found: " + splitString[i]);
// Do whatever you need to do
}

Categories

Resources