user5164784
user5164784

Reputation:

setText method not working properly?

I am trying to execute the settext method on my textview but it doesn't seem to work. I learned that "%s" in the first argument of the string.format method should return the given string of the second argument but it somehow it doesn't work.

SPCalories = Double.longBitsToDouble(sharedPreferences.getLong("Calories", Double.doubleToLongBits(0)));
d_c = SPCalories;
dc_text = Double.toString(d_c);
Calories_text.setText(R.string.Calories + String.format("%s", dc_text));

R.string.Calories:

<string name="Calories">Calories: </string>

Am I doing something wrong with SharedPreferences maybe?

Upvotes: 0

Views: 223

Answers (2)

Mike
Mike

Reputation: 4570

You should do:

<string name="Calories">Calories: %s</string>

And:

Calories_text.setText(String.format(getString(R.string.Calories), dc_text));

Upvotes: 1

Ziem
Ziem

Reputation: 6697

You omitted getString method:

Calories_text.setText(getString(R.string.Calories) + String.format("%s", dc_text));

If you wold like to learn more about formatting strings check: http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling.

Upvotes: 0

Related Questions