TypeArray в Android - Как сохранить пользовательский объекты в xml и получить их?

У меня есть такой класс, как

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

, и я хочу хранить объекты этого класса в TypeArray xml Android, например

<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 вроде

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); но он дает мне все как строку Албания al @ drawable / al

Пожалуйста, помогите мне решить эту проблему.

Заранее большое спасибо,

С уважением, Ishan

20
задан smac89 18 May 2015 в 22:27
поделиться