Frank
Frank

Reputation: 12300

Android Custom XML Objects

Once in a while I want to populate a list with items that are static, what/where is the best way to store those static items?

I normally use an xml resource with something like this:

<string-array name="category_ids">
    <item>0</item>
    <item>1</item>
    <item>2</item>
</string-array>

<array name="category_titles">
    <item>@string/category_all</item>
    <item>@string/category_new</item>
    <item>@string/category_hot</item>
</array>

<array name="category_icon_ids">
    <item>@null</item>
    <item>@drawable/ic_category_star</item>
    <item>@drawable/ic_category_thumb</item>
</array>

I access these arrays and populate an adapter with custom objects. But it annoys me every time I use it. When I change the order in the list for example, I have to change every array in the xml.

Is there a better solution? Are custom XML objects supported?

Upvotes: 0

Views: 526

Answers (2)

Machado
Machado

Reputation: 14489

Sometimes when working with Custom Objects that can vary, accessing and populating these items with an adapter can be very stressful.

Instead, you can use json to handle more complex objects than with simple arrays.

Here's an example of a json implementation:

In the /res/raw folder:

{ "countries" : [
    {"country" : "Albania", "countryCode" : "al" },
    {"country" : "Algeria", "countryCode" : "dz"},
    {"country" : "American Samoa", "countryCode" : "as"},
    {"country" : "India", "countryCode" : "in"},
    {"country" : "South Africa", "countryCode" : "sa"}
]}

Loading data in your class:

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

If you don't want to do the parsing manually you can use gson to pass the objects and load the drawables.

Source: https://stackoverflow.com/a/9809772/1549700

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157437

yes, you can have your custom xml, stored under res/xml/, but in this case accessing the content is not exactly free. You can use getResources().getXml(R.xml.name_of_file), to retrieve an instance of XmlResourceParser, which you have to use to parse the xml

Upvotes: 2

Related Questions