employeegts
employeegts

Reputation: 144

String resource showing incorrect value

I have following string resource defined in strings.xml

<string name="years">Years</string>

In following 2 ways i am trying to use it

showUserSelectedExperience.setText("0"+R.string.years);
showUserSelectedExperience.setText(String.valueOf(experience.getProgress())+R.string.years);

Following incorrect output i am getting

02131361882

But correct output i should get : 0 years, 1 years like that

what can be the problem and what are the possible solutions?

Upvotes: 1

Views: 638

Answers (3)

Inneke De Clippel
Inneke De Clippel

Reputation: 1026

When you use showUserSelectedExperience.setText(R.string.years);, it will show 'Years'. That is because an integer is found (2131361882) and the correct string resource will be returned. Now, if you use this string: "0"+R.string.years, it's the same as concatenating the String "0" with another String. The same way that "Year "+1 will output "Year 1". The integer R.string.years, which has 2131361882 as value, is parsed to a String with value "2131361882". That is why you are getting this result. So short: setText(integer) will get the string resource, setText(string) will show that string value. The solution is to first get the string resource, and then concatenate it.

String years = getString(R.string.years);
showUserSelectedExperience.setText("0"+years);

Same as the other answers.

Upvotes: 2

Kastriot Dreshaj
Kastriot Dreshaj

Reputation: 1121

You should use this

showUserSelectedExperience.setText("0" + getString(R.string.years));

Upvotes: 1

Tapa Save
Tapa Save

Reputation: 4857

Use

 showUserSelectedExperience.setText("0"+getResources().getString( R.string.years));

Upvotes: 3

Related Questions