Android中的TypeArray-如何在xml中存储自定义对象并检索它们?

我有一门课

公共类CountryVO{
私有字符串国家代码;
私有字符串countryName;
私人可拉国旗;
公共字符串getCountryCode(){
返回国家代码;
}
公共无效setCountryCode(字符串countryCode){
this.countryCode=countryCode;
}
公共字符串getCountryName(){
返回国家名称;
}
public void setCountryName(字符串countryName){
this.countryName=countryName;
}
公共可绘制getCountryFlag(){
返回国旗;
}
公共无效设置countryFlag(可提取的countryFlag){
this.countryFlag=countryFlag;
}
}

并希望将此类对象存储在类似android的TypeArray xml中

<资源>
<数组名称=“0”;定制“arr”&燃气轮机;
<项目>
<countryName>阿尔巴尼亚&lt/countryName>
<国家代码>al&lt/国家代码>
<国旗&[email protected]可拉拔/铝合金&lt/国旗>
&lt/项目>
<项目>
<countryName>阿尔及利亚&lt/countryName>
<国家代码>dz&lt/国家代码>
<国旗&[email protected]可抽出式/dz&lt/国旗>
&lt/项目>
<项目>
<countryName>美属萨摩亚&lt/countryName>
<国家代码>as&lt/国家代码>
<国旗&[email protected]可抽出式/as&lt/国旗>
&lt/项目>
<项目>
<countryName>印度&lt/countryName>
<国家代码>在&lt/国家代码>
<国旗&[email protected]可抽出式/插入式&lt/国旗>
&lt/项目>
<项目>
<countryName>南非&lt/countryName>
<国家代码>sa&lt/国家代码>
<国旗&[email protected]可抽出式/sa&lt/国旗>
&lt/项目>
&lt/阵列>
&lt/资源>

如何在Activty类中访问此数组,如

TypedArray customArr=getResources().obtainTypedArray(R.array.country\u arr);
CountryVO=新CountryVO();
vo.setCountryName(**数组中的值用于第一个元素的countryName属性**);
vo.setCountryCode(**数组中的值用于第一个元素的countryCode属性**);
vo.setCountryFlag(**数组中的值用于第一个元素的countryFlag属性**);

但我不知道如何做到这一点。
我尝试了customArr.getString(0);但它给我的一切就像绳子一样
阿尔巴尼亚[email protected]/al

请帮我解决这个问题

提前多谢了

致以最良好的祝愿,
伊山

下面是一个例子。阅读并查看Darray类型的方法,如get…()例如getDrawable(int index)。我建议将相同类型的项保存在单独的数组中

<数组名称=“国家/地区”>
<项目>阿尔巴尼亚&lt/项目>
<项目>阿尔及利亚&lt/项目>
<项目>美属萨摩亚&lt/项目>
&lt/阵列>
<数组名称=“代码”>
<项目>al&lt/项目>
<项目>dz&lt/项目>
<项目>as&lt/项目>
&lt/阵列>
<array name=“flag”>
<项目&[email protected]可抽出式/dz&lt/项目>
<项目&[email protected]可拉拔/铝合金&lt/项目>
<项目&[email protected]可抽出式/as&lt/项目>
&lt/阵列>

编辑:

公共CountryVO getCountryVO(int索引){
Resources=getResources();
TypedArray country=resources.obtainTypedArray(R.array.country);
TypedArray代码=resources.obtainTypedArray(R.array.code);
TypedArray标志=resources.obtainTypedArray(R.array.flag);
CountryVO=新的CountryVO(country.getString(索引)、code.getString(索引)、flag.getDrawable(索引));
国家。回收();
code.recycle();
flag.recycle();
返回vo;
}

发表评论