Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from android string array from resource

I know how to get data from string-array. I have done like this.

<string-array name="values">
   <item>value1</item>
   <item>value2</item>
</string-array>

String[] values = this.getResources().getStringArray(R.array.values);

But I dont know whether the below type string-array is possible and confused to get values from that.

<string-array name="country_data">
        <item>
            <country>Afghanistan</country>
            <countryCode>93</countryCode>
            <iso2>AF</iso2>
            <iso3>AFG</iso3>
        </item>
    </string-array>

If it is possible please help me to get the values.

like image 316
Alex Chengalan Avatar asked May 22 '26 21:05

Alex Chengalan


1 Answers

I dont know whether the below type string-array is possible

It is not.

If you want to have arbitrary XML, use res/xml/ for whatever XML structure you like. You can then use getResources().getXml() to get an XmlPullParser that you can use to parse that XML.

Or, wrap the contents of each of your <item> elements in CDATA, which should prevent the resource parser from messing with them. Then, each string you retrieve from the array would be plain XML, that you would need to parse.

Or, go with JSON in res/raw/ or assets/, using your favorite JSON parser (e.g., Gson).

Or, use SQLiteAssetHelper to package a database in your app that has this data pre-loaded.

Or, use R. Zagórski's solution, of having one <string-array> per object, with <item> elements for each field of the object.

like image 197
CommonsWare Avatar answered May 25 '26 09:05

CommonsWare