Suppose I have stored a 2 dimensional array in android resource as shown below. How can I get them in a java collection like Arraylist?
<resources>
<string-array name="countries_array">
<item>
<name>Bahrain</name>
<code>12345</code>
</item>
<item>
<name>Bangladesh</name>
<code>54545</code>
</item>
<item>
<name>India</name>
<code>54455</code>
</item>
</string-array>
</resources>
For example in case of 1 dimensional array we can do it using
getResources().getStringArray(R.array.countries_array);
When the countries_array is like
<resources>
<string-array name="countries_array">
<item>Bahrain</item>
<item>Bangladesh</item>
<item>India</item>
</string-array>
</resources>
The <string-array>
element of a resources file can only be used for single dimension arrays. In other words, everything between <item>
and </item>
is considered to be a single string.
If you want to store data in the way you describe (effectively pseudo-XML), you'll need to get the items as a single String[]
using getStringArray(...)
and parse the <name>
and <codes>
elements yourself.
Personally I'd possibly go with a de-limited format such as...
<item>Bahrain,12345</item>
...then just use split(...)
.
Alternatively, define each <item>
as a JSONObject such as...
<item>{"name":"Bahrain","code":"12345"}</item>