Note that there are some explanatory texts on larger screens.

plurals
  1. POTypeArray in Android - How to store custom objects in xml and retrieve them?
    primarykey
    data
    text
    <p>I have a class like</p> <pre><code>public class CountryVO { private String countryCode; private String countryName; private Drawable countryFlag; public String getCountryCode() { return countryCode; } public void setCountryCode(String countryCode) { this.countryCode = countryCode; } public String getCountryName() { return countryName; } public void setCountryName(String countryName) { this.countryName = countryName; } public Drawable getCountryFlag() { return countryFlag; } public void setCountryFlag(Drawable countryFlag) { this.countryFlag = countryFlag; } } </code></pre> <p>and want to store objects of this class in an TypeArray xml of android like</p> <pre><code>&lt;resources&gt; &lt;array name="custom_arr"&gt; &lt;item&gt; &lt;countryName&gt;Albania&lt;/countryName&gt; &lt;countryCode&gt;al&lt;/countryCode&gt; &lt;countryFlag&gt;@drawable/al&lt;/countryFlag&gt; &lt;/item&gt; &lt;item&gt; &lt;countryName&gt;Algeria&lt;/countryName&gt; &lt;countryCode&gt;dz&lt;/countryCode&gt; &lt;countryFlag&gt;@drawable/dz&lt;/countryFlag&gt; &lt;/item&gt; &lt;item&gt; &lt;countryName&gt;American Samoa&lt;/countryName&gt; &lt;countryCode&gt;as&lt;/countryCode&gt; &lt;countryFlag&gt;@drawable/as&lt;/countryFlag&gt; &lt;/item&gt; &lt;item&gt; &lt;countryName&gt;India&lt;/countryName&gt; &lt;countryCode&gt;in&lt;/countryCode&gt; &lt;countryFlag&gt;@drawable/in&lt;/countryFlag&gt; &lt;/item&gt; &lt;item&gt; &lt;countryName&gt;South Africa&lt;/countryName&gt; &lt;countryCode&gt;sa&lt;/countryCode&gt; &lt;countryFlag&gt;@drawable/sa&lt;/countryFlag&gt; &lt;/item&gt; &lt;/array&gt; &lt;/resources&gt; </code></pre> <p>how i want to access this array in my Activty class like</p> <pre><code>TypedArray customArr = getResources().obtainTypedArray(R.array.country_arr); CountryV) vo = new CountryVO(); vo.setCountryName(**value from array come here for first element's countryName attribute**); vo.setCountryCode(**value from array come here for first element's countryCode attribute**); vo.setCountryFlag(**value from array come here for first element's countryFlag attribute**); </code></pre> <p>But i don't to how to achieve this. I tried customArr.getString(0); but it gives me everything as string like Albania al @drawable/al</p> <p>Please help me to solve this problem.</p> <p>Thanks a lot in advance,</p> <p>With best regards, Ishan</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload