January 9, 2011

[SOLVED] Android: Get an string array from an array.xml resource

Here you can see how to retrieve values from array.xml
Log.i("test",getImageQualityAsString(1));

array.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="image_export_list_preference">
<item>480x320</item>
<item>800x480</item>
<item>1024x768</item>
</string-array>
<string-array name="entryvalues_image_export_list_preference">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
</resources>

Function:
private String getImageQualityAsString(int sizeItem){

try {
String[] bases = getResources().getStringArray(R.array.image_export_list_preference);

return bases[sizeItem];

} catch (Exception e) {
return "";
}

}

Source: stackoverflow.com

2 comments:

  1. Thanks, exactly what I needed. Well very nearly ...

    I made it a bit more generic by including the array's ID as a parameter.

    private String getStringFromArray(int id, int index) {
    try {
    String[] bases = getResources().getStringArray(id);
    return bases[index];
    } catch (Exception e) {
    return "";
    }
    }

    Called by
    Log.i("test",getStringFromArray(R.array.image_export_list_preference, 1));

    ReplyDelete
  2. i am having multiple checkboxes in first activity, if i select any of the checkbox i am placing some related data into stringarray, and on click of submit, i need to display the stringarray in the next activity

    please send me the code

    ReplyDelete