Something weird about string resources

I am using string resources (as someone on these forums recommended them for memory efficiency) and I see some weird behavior. I have my string resources like this:

<strings>
<string id="AppName">PacerField</string>

<string id="label">Lap</string>
<string id="label2">Avg Pace T: </string>
</strings>

In my DataField I want to update the label for average pace to include the target pace (hence the T: ) so in onUpdate() I do this:

label2Field.setText(Rez.Strings.label2+expectedAvgPaceString);

Let's say expectedAvgPaceString is something like "7:35", calculated in compute(), I get "9457:35" for my label2!
If I leave out the +expectedAvgPaceString I get "Avg Pace T:".

To start understanding what was going on I tried a few other strings including "", I always get 945 instead of my string.

When I tried using System.println() I got the following:

In the code:
System.println(Rez.Strings.label2);
System.println(Rez.Strings.label2+"");
System.println(Rez.Strings.label2.toString());

Console output:
945
945
945

Anyone have an explanation? Also, I can make all of this work by using explicit strings in the code, but it uses more memory and is less portable if I ever decided to make other language versions.

Thanks,

Gerald
  • Instead of using

    Rez.Strings.label2

    you want to use

    Ui.loadResource(Rez.Strings.label2)

    You're not actually loading the string
  • It would seriously surprise me that resource strings are more memory efficient than hardcoded strings....
  • label2Field.setText(Rez.Strings.label2);


    This works because setText() takes either a String or a resourceId (a Number). This is expected behavior.

    label2Field.setText(Rez.Strings.label2+expectedAvgPaceString);


    This does not work because you are adding a resourceId (a Number) to a String and then passing the resulting String to setText().

    If you want to build up a String and use that, you have to convert the resourceId to a String first, then do your dirty work.. as suggested by Jim above.

    label2Field.setText(WatchUi.loadResource(Rez.Strings.label2)+ expectedAvgPaceString);


    Travis