Troubleshooting guide

240
BlackBerry Java Development Environment Development Guide
Retrieve strings from a resource file
Code sample: Retrieving strings from a resource file
Example: CountryInfo.java (with localization support)
/**
* CountryInfo.java
* Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved.
*/
package com.rim.samples.docs.countryinfo;
import net.rim.device.api.ui.*;
Task Steps
Retrieve the resource bundle. When the BlackBerry Integrated Development Environment builds your project, it creates an
interface for each .rrh resource file.
1. Import the interfaces that the BlackBerry IDE creates.
import com.rim.samples.docs.resource.CountryInfoResource;
2. Create a ResourceBundle object to contain the localized resources, such as strings, for an
application.
private static ResourceBundle _resources =
ResourceBundle.getBundle(CountryInfoResource.BUNDLE_ID,
CountryInfoResource.BUNDLE_NAME);
3. To retrieve the appropriate bundle family, invoke getBundle().
Create menu items using resources. >Use the MenuItem constructor that accepts a resource bundle and a resource instead of a
String for the name of the menu item.
private MenuItem _viewItem = new MenuItem(_resources, MENUITEM_VIEW,
110, 10) {
public void run() {
select = choiceField.getSelectedIndex();
_infoScreen = new InfoScreen();
UiApplication.getUiApplication().pushScreen(_infoScreen);
}
}
Replace text strings with the appropriate
resources.
1. For each field that appears on the main screen, replace the text string with the appropriate
resource.
2. Invoke getString() or getStringArray() to retrieve the string for the appropriate
language.
LabelField title = new
LabelField(_resources.getString(APPLICATION_TITLE),
LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
add(new RichTextField(_resources.getString(FIELD_TITLE)));
String choices[] = _resources.getStringArray(FIELD_COUNTRIES);
choiceField = new ObjectChoiceField(_resources.getString(FIELD_CHOICE),
choices);