Welcome!

Welcome to the Official BlackBerry Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Contributor
ankith13
Posts: 14
Registered: ‎03-29-2012
My Carrier: Airtel
Accepted Solution

Get a resource using a string key

Hi

 How do I get a String array Resource using a string key...

for Example I have 4 String arrays defined in the resources.

d1, d2, d3, d4

 

How can I get these resource like.

 

private static ResourceBundle nameResources = ResourceBundle.getBundle( BUNDLE_ID, BUNDLE_NAME);

 

String[] Array = nameResources .getStringArray(KeyName);

 

instead of giving keyname d1 can I somehow give "d"+1 (so that I can loop resources)?

Please use plain text.
Developer
simon_hain
Posts: 13,817
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Get a resource using a string key

welcome to the support forums.
you can use whatever key you like, but you'll get a missingresourceexception if the index you use does not exist.
you can open the rrh file in a text editor to see which keys are used where.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
ankith13
Posts: 14
Registered: ‎03-29-2012
My Carrier: Airtel

Re: Get a resource using a string key

Thank you for your reply.. I have already achieved some basic knowledge in resources.. I am also successfully load resource in the code...
But my problem is - (without geting an exception) How do I load a resource by giving a String as a key rather than giving the name of the resource...
something like-
String[] Array = nameResources .getStringArray("KeyName");

rather than-
String[] Array = nameResources .getStringArray(KeyName);
Please use plain text.
Developer
simon_hain
Posts: 13,817
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: Get a resource using a string key

you don't, there is no API for that.
see
http://www.blackberry.com/developers/docs/7.1.0api/net/rim/device/api/i18n/ResourceBundle.html
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Developer
peter_strange
Posts: 17,713
Registered: ‎07-14-2008

Re: Get a resource using a string key

[ Edited ]

It seems that you wish to have the benefit of using the localization, but also to make the code that chooses the localization key dynamic.

 

You will have to introduce this dynamic change yourself.  Here is an example (not your exact example, but something I hope you can extend to meet your needs). 

 

Assume that you have a choice of 3 messages, and the one you choose us determined by the messageIndex variable you set which can have the value 0, 1 or 2.  The messages are in the resources and have the ids

MESSAGE_ZERO

MESSAGE_ONE

MESSAGE_TWO

 

In your program, you can do the following:

 

private static ResourceBundle nameResources = ResourceBundle.getBundle( BUNDLE_ID, BUNDLE_NAME);

 

private static String [] messages = new String {

    nameResources.getString(MESSAGE_ZERO),

    nameResources.getString(MESSAGE_ONE),

    nameResources.getString(MESSAGE_TWO)

}

 

....

 

String thisMessage = messages[messageIndex];

 

 

Edit:

 

If you wished to extend this so that you could use a String as the 'index', then the easiest option would be to replace the messages array in the sample above with a Hashtable, and populate the Hashtable using

- the Strings that you want to search on as keys, and

- the values extracted from the resource bundle as the data. 

 

Hope this makes sense.

Please use plain text.