我有一个类似这样的类:
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;
}
}
我想将这个类的对象存储在 Android 的 TypeArray xml 中,例如:
<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>
我想在我的Activity类中访问这个数组,应该怎么做?
TypedArray customArr = getResources().obtainTypedArray(R.array.country_arr);
CountryVO 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**);
但我不知道如何实现这一点。 我尝试了customArr.getString(0);但它给了我一个字符串,类似于 阿尔巴尼亚 al @drawable/al
请帮我解决这个问题。
非常感谢您的帮助,
致以最诚挚的问候, Ishan
无法解析方法 convertStreamToString
。我尝试导入所有可用选项。 - wick.ed