Problem with mixing String from XML with String

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Problem with mixing String from XML with String

Postby E-Nature » Fri Jan 01, 2010 7:21 pm

I have a popup with comes when there are no search results after a user search query. I want to load the error text from values/strings.xml - this all works wonderfully with no problem. The thing is that I want to load the error text from strings.xml and add the search query the user entered behind that. The odd thing is, if I mix that together then I do not get the actual text value, instead I get an Integer value. I have no idea why it suddenly delivery a number instead of the text I put into strings.xml.

strings.xml:
<string name="errorMsgTitle">Nothing found with your keywords</string>

This works:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. errorMsgTitle.setText(R.string.errorMsgTitle);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4

Output: Nothing found with your keywords

and this does not work:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. errorMsgTitle.setText(R.string.errorMsgTitle + " '" + gameEntry.getText().toString() + "'");
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

Output: 2131034125 'search query'

Is this normal or a bug? how can I work around this? The solution output should be:
Nothing found with your keywords 'search query'

thank you :)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Postby jwei512 » Fri Jan 01, 2010 10:00 pm

So the TextView for Android actually has two ways of setting text if you look at the docs. One way is

setText(int resid) and the other is setText(CharSequence text)

Your R.string.errorMsgTitle is actually just an integer id that references the string in your string.xml (these id's are generated in your R.java file under your "gen" folder in your project), so when you do:

errorMsgTitle.setText(R.string.errorMsgTitle);

You're actually calling the former version of setText, and when you do:

errorMsgTitle.setText(R.string.errorMsgTitle + " '" + gameEntry.getText().toString() + "'");

You're calling the latter and so the compiler just interprets R.string.errorMsgTitle to be an integer converted to a string.

Make sense?
- jwei

http://thinkandroid.wordpress.com
Examples, Code, and Tutorials
jwei512
Senior Developer
Senior Developer
 
Posts: 135
Joined: Mon Jul 27, 2009 9:35 pm
Location: Palo Alto, CA

Postby E-Nature » Sat Jan 02, 2010 2:15 pm

thanks for the explanation. yes, it makes sense to me :)

I found a working solution:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. errorMsgTitle.setText(R.string.errorMsgTitle);
  3.  
  4. errorMsgTitle.setText(errorMsgTitle.getText() + " '" + searchQuery + "'.");
  5.  
  6.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


although I must say that I think this is not really a good solution. but at least it works.
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Postby grzegorz.zajac » Sat Jan 02, 2010 4:02 pm

Hi,

You can also try this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. errorMsgTitle.setText(getResources().getString(R.string.errorMsgTitle) + " '" + searchQuery + "'.");
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Regards,
Grzesiek
grzegorz.zajac
Developer
Developer
 
Posts: 39
Joined: Wed Feb 18, 2009 8:37 pm
Location: Krakow/Poland

Postby E-Nature » Sat Jan 02, 2010 8:21 pm

thanks, much better :-)
User avatar
E-Nature
Experienced Developer
Experienced Developer
 
Posts: 93
Joined: Mon Nov 16, 2009 3:39 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 3 guests