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

Ish*_*han 20 xml android custom-component typedarray

我有一个类似的课程

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并希望将此类的对象存储在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>
Run Code Online (Sandbox Code Playgroud)

我想如何在我的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**);
Run Code Online (Sandbox Code Playgroud)

但我不知道如何实现这一目标.我试过customArr.getString(0); 但它给了我像阿尔巴尼亚al @ drawable/al一样的字符串

请帮我解决这个问题.

非常感谢提前,

最诚挚的问候,伊森

paw*_*eba 15

这是一个例子.阅读它并查看TypedArray的方法,get...()例如getDrawable(int index).我建议将相同类型的项目保存在分离的数组中.

<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>
Run Code Online (Sandbox Code Playgroud)

编辑:

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;
}
Run Code Online (Sandbox Code Playgroud)


pab*_*sco 15

当我需要可以在代码外编辑的自定义对象时,我通常使用json,这对于人类和(可能)机器都更容易阅读;)

与简单数组相比,您还可以拥有更复杂的对象.

/res/raw文件夹中创建一个json文件(例如countries.json),如下所示:

{ "countries" : [
    {"country" : "Albania", "countryCode" : "al" },
    {"country" : "Algeria", "countryCode" : "dz"},
    {"country" : "American Samoa", "countryCode" : "as"},
    {"country" : "India", "countryCode" : "in"},
    {"country" : "South Africa", "countryCode" : "sa"}
]}
Run Code Online (Sandbox Code Playgroud)

你可以像这样加载数据:

InputStream jsonStream = context.getResources().openRawResource(R.raw.countries);
JSONObject jsonObject = new JSONObject(Strings.convertStreamToString(jsonStream));
JSONArray jsonContries = jsonObject.getJSONArray("countries");
List<CountryVO> countries = new ArrayList<CountryVO>();
for (int i = 0, m = countries.length(); i < m; i++) {
    JSONObject jsonCountry = countries.getJSONObject(i);
    CountryVO country = new CountryVO();
    country.setCountryName(jsonCountry.getString("country"));
    String co = jsonCountry.getString("countryCode");
    country.setCountryCode(co);
    try {
        Class<?> drawableClass = com.example.R.drawable.class; // replace package
        Field drawableField = drawableClass.getField(co);
        int drawableId = (Integer)drawableField.get(null);
        Drawable drawable = getResources().getDrawable(drawableId);
        country.setCountryFlag(drawable);
     } catch (Exception e) {
         // report exception
     }
     countries.add(country);
}
Run Code Online (Sandbox Code Playgroud)

如果你不想手动解析,你也可以使用gson帮助你传递对象,然后以懒惰的方式加载drawable ... ...)

编辑:添加实用程序类

public String convertStreamToString(InputStream is) { 
  Scanner s = new Scanner(is).useDelimiter("\\A");
  return s.hasNext() ? s.next() : "";
}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你


Ken*_*nny 0

在尝试将 xml 存储到类中之前,您需要解析它。我建议您使用 SAX API,您可以在此处找到有关它的教程。希望这可以帮助!