Search code examples
androidxmlstringandroid-xmlmaintainability

How to combine sets of Images, Strings, and Integers, in an easy to maintain format


So, I'm trying to figure out the best way to combine many data types together. Internal to my code, I'm creating a class. Externally, I want to have a single place to go to manage to following types of data, or at least pointers to said data. Note that there is a lot of sets of these data, and I anticipate adding more as time continues.

EDIT: I'm massively simplifying what was once here.

Imaging that at compile time, I have a list of objects. Each object has some characteristic, which all objects will have in common. These include:

  • An imageImage
  • Some strings
  • An array of Integers
  • An array of strings

I am wanting to find a way to store all of these together outside of any code. Ideally, I would like to use XML, with something that vaguely looks like this:

<storage_type>
   <image resid="@id/image1" />
   <name>Item_Name1</name>
</storage_type>
<storage_type>
   <image resid="@id/image2" />
   <name>Item_Name2</name>
</storage_type>

The images are currently stored as resource variables. I'm just not sure how I could put a reference to an image into the XML file.

I am not married to the idea of XML files, if a better solution can be found. All I want is a single database of some kind a way to access this information.

This data should not be changed, unless I submit a new update. These do not need to be updated at runtime, but I definitely would like to update them between builds, from time to time.. I've heard of Preferences, which might work, but I'm unsure if they might be user updatable somehow.


Solution

  • I was able to figure out a solution, with considerable help from this question.

    1. My data is stored in an XML array.
    2. This data contains an array index which points to an image array, maintained under a normal resource.

    My data storage file looks like:

    <storage_type>
       <image imageArrayIndex="1" />
       <name>Item_Name1</name>
    </storage_type>
    <storage_type>
       <image imageArrayIndex="2" />
       <name>Item_Name2</name>
    </storage_type>
    

    I have a file in my res/values folder that looks like:

    <resources>
        <string-array name="images">
            <item>@drawable/default</item>
            <item>@drawable/image1</item>
            <item>@drawable/image2</item>
        </string-array>
    </resources>
    

    And somewhere deep in parsing XML code I have:

    TypedArray imgs = context.getResources().obtainTypedArray(R.array.achievement_images);
    //Whole bunch of code to parse XML, eventually coming to this line:
    else if (name.equals("image"))
    {
      imageRes=imgs.getResourceId(xpp.getAttributeIntValue(null,"imageArrayIndex", 0),-1);
    }
    

    Alternatively, I could simply store the name of the image like below:

    <storage_type>
       <image resid="image1" />
       <name>Item_Name1</name>
    </storage_type>
    <storage_type>
       <image resid="image2" />
       <name>Item_Name2</name>
    </storage_type>
    

    And then get it by parsing the XML tag, and getting the resource ID by:

    int resid=Context.getResources().getIdentifier (name_string_Image, "drawable", null);