TypeArray在Android的 - 如何存储自定义的XML对象并获取它们呢?自定义、对象、TypeArray、Android

2023-09-13 00:24:32 作者:颜色不一样的烟火 @

我有一类像

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像

and want to store objects of this class in an 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>

我多么想访问该阵列中的我Activty类像

how i want to access this array in my Activty class like

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**);

但我不如何实现这一目标。 我想customArr.getString(0);但它给了我一切,串状 阿尔巴尼亚人@绘制/人

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

请帮我解决这个问题。

感谢很多提前,

与问候, 依禅

推荐答案

下面是例子。读它,并期待在TypedArray像的get ...的方法()例如 getDrawable(INT指数)。我会建议保持相同类型的项目中分离阵列。

Here is example. Read it and look at the methods of TypedArray like get...() for example getDrawable(int index). I would suggest to keep items of the 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>

编辑:

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