TypeArray in Android - How to store custom objects in xml and retrieve them? -


i have class like

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;     } } 

and want store objects of class in typearray xml of android like

<resources>     <array name="custom_arr">         <item>             <countryname>albania</countryname>             <countrycode>al</countrycode>             <countryflag>@drawable/al</countryflag>         </item>         <item>             <countryname>algeria</countryname>             <countrycode>dz</countrycode>             <countryflag>@drawable/dz</countryflag>         </item>         <item>             <countryname>american samoa</countryname>             <countrycode>as</countrycode>             <countryflag>@drawable/as</countryflag>         </item>         <item>             <countryname>india</countryname>             <countrycode>in</countrycode>             <countryflag>@drawable/in</countryflag>         </item>         <item>             <countryname>south africa</countryname>             <countrycode>sa</countrycode>             <countryflag>@drawable/sa</countryflag>         </item>     </array> </resources> 

how want access array in activty class like

typedarray customarr = getresources().obtaintypedarray(r.array.country_arr);     countryv) vo = new countryvo();     vo.setcountryname(**value array come here first element's countryname attribute**);     vo.setcountrycode(**value array come here first element's countrycode attribute**);     vo.setcountryflag(**value array come here first element's countryflag attribute**); 

but don't how achieve this. tried customarr.getstring(0); gives me string albania al @drawable/al

please me solve problem.

thanks lot in advance,

with best regards, ishan

here example. read , @ methods of typedarray get...() example getdrawable(int index). suggest keep items of same type in separated arrays.

<array name="country">     <item>albania</item>     <item>algeria</item>     <item>american samoa</item> </array> <array name="code">     <item>al</item>     <item>dz</item>     <item>as</item> </array> <array name="flag">     <item>@drawable/dz</item>     <item>@drawable/al</item>     <item>@drawable/as</item> </array> 

edit:

public countryvo getcountryvo(int index){     resources resources = getresources();     typedarray country = resources.obtaintypedarray(r.array.country);     typedarray code = resources.obtaintypedarray(r.array.code);     typedarray flag = resources.obtaintypedarray(r.array.flag);      countryvo vo = new countryvo(country.getstring(index), code.getstring(index), flag.getdrawable(index));      country.recycle();     code.recycle();     flag.recycle();      return vo; } 

Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -