Java – Load bundles to/from files on Android

Load bundles to/from files on Android… here is a solution to the problem.

Load bundles to/from files on Android

I’m moving to doing some Android coding, and what I often do is create plists that contain my static list items. This is useful when I have to do multiple layers of ListView where users can drill down to find information. (e.g. Continent –> Country –> City) Then what I did was write a ListView that drilled its own copy deeper into the data tree.

However, on Android, I can’t seem to find any kind of plist that would allow me to do the above (without creating a SQL database).

So what I ended up doing was hardcoding everything… That’s not what I want to do. Android has a ton of xml resources available, and I feel like I should follow this pattern.

ArrayList<Bundle> data = new ArrayList<Bundle>();

Bundle infoHeader = new Bundle();
infoHeader.putInt(keyText,R.string.stay_informed);
infoHeader.putInt(keyLayout,R.layout.main_header);
data.add(infoHeader);

Bundle readAction = new Bundle();
readAction.putInt(keyText,R.string.read);
readAction.putInt(keySubtext, R.string.read_substr);
readAction.putInt(keyLayout, R.layout.main_row_blue);
 link to new ArrayList of items for the next level down
data.add(readAction);

I want to do the following (or something very similar):

<resources>
<array name="main_list">
    <extra>
        <string name="keyText" value="@string/stay_informed" />
        <layout name="keyLayout" value="@layout/main_header" />
    </extra>
    <extra>
        <string name="keyText" value="@string/read" />
        <string name="keySubtext" value="@string/read_substr" />
        <layout name="keyLayout" value="@layout/main_row_blue" />
        <!-- <string name="keyLink" value="@array/detail_list" /> Where detail_list has more Bundles -->
    </extra>
</array>
</resource>

Simply load the bundle array at runtime instead of hard-coding it into your app.

So. How do I efficiently store/retrieve the NSArray equivalent of NSDictionaries on Android?

(I found android plist parser, but I wondered how.) Complete it on Android)

Solution

Well, I’ll give it a try. I’m currently moving from Android to iOS (the exact opposite of you), so I understand what you’re trying to do (I think). Java doesn’t have a “dictionary”, but it does have HashMap (key/value pairing – same thing). If you really want to, you can use serialization to save your objects to a file that you can package into Android’s Assets folder. You can then read (but not write) this information at any time.

Here is a link to help you serialize Java objects. Search for HashMaps in Java and you’ll see a lot of things. If you plan to place many different types of objects in this HashMap, don’t use generics. (They look like this <String, String>). Leave those to make an original HashMap.

Serialization: (when you stick to it)
http://www.javadb.com/writing-objects-to-file-with-objectoutputstream

Deserialization: (when you read it back)
http://www.exampledepot.com/egs/java.io/DeserializeObj.html

Another potentially simpler approach is to just create an XML file (like you did above) and store it in Assets. You can then parse it using XMLPullParser when the app starts and store it in memory while the app is running, but you won’t be able to use “@layout, @string”, etc.

Related Problems and Solutions