views:

142

answers:

2

I'm new to Android development, and I've been playing around with it a bit. I was trying to create a program that has a small database-like collection of never-changing data. In C#, my currently best language, I'd use a List of a custom class and serialize that to an xml file, then read that into my application at runtime. I found the /xml resource folder in Android, but I'm not sure how I would go about doing what I'm envisioning. What would be the best way to go about doing this?

The data will never need to change. Example:

Blob   | A  | B
----------------
Blob 1 | 23 | 42
Blob 2 | 34 | 21

I know that's laid out like a table, but using a database doesn't really make sense to me because the data will never change, and I would need a way to store it to initially populate the database anyway.

So basically I'm looking for a way to store somewhat-complex static data in my application. Any ideas?

EDIT: I also saw the /raw folder. So I could store things in /res/raw or /res/xml. But I'm not sure what would be the best way to store/parse the data...

+2  A: 

The best way is to use the Android Resource Heirarchy.

In the res/values/ directory, you can store any number of key-value pairs for several basic data types. In your app, you would refer to them using an autogenerated resource id (name based on your resource's key). See the link above for more documentation and details.

Android also supports raw datafiles. You could store your data in the file directory under res/raw/yourfile.dat

You you create your data in whatever text based format you want and then read it on activity startup using the resource access apis.

CodeFusionMobile
would that be better or worse than storing it in an xml form and reading it in? and if I did use an xml form, how would I read it in?
NickAldwin
@NickAldwin Better in that you could store in any format you want, worse in you don't have built in APIs for parsing the data. You can also use the resources hierarchy which would be better. I'll edit my post with the details.
CodeFusionMobile
What I'm worried about with the res/values/ is that I don't plan on just storing data in one-to-one pairs (as in the example above). So are there built in apis for parsing xml files other than those in the hierarchy specified in the link you posted? EDIT: Would TypedArray work for a custom class (like the "Blob" example above)? Would that be the way to go?
NickAldwin
@NickAldwin Yes there is. "http://developer.android.com/reference/android/content/res/Resources.html#getXml(int)"
CodeFusionMobile
Ah, I had seen that before but hadn't looked too deeply into it. Thanks! So that is a good way to go then. I didn't want to break any conventions...
NickAldwin
+3  A: 

I think this is the BEST solution and i am already using this one to store Static-data in my every project.

For that... You can do one thing, make one xml file namely "temp.xml" ..and store the data in temp.xml as follows:

  <?xml version="1.0" encoding="utf-8"?> 
<rootelement1>

    <subelement> Blob 1  
     <subsubelement> 23 </subsubelement>
     <subsubelement> 42 </subsubelement> 
    </subelement>

    <subelement>Blob 2      
    <subsubelement> 34 </subsubelement>
    <subsubelement> 21 </subsubelement>
    </subelement>


    </rootelement1>

and then use XML PullParser technique to parse data. You can have coding samples of PullParsing technique on Example , refer this example for better idea.

Enjoy!!

PM - Paresh Mayani
Yep, that's the technique I ended up going with, as discussed on the comments of the accepted answer. I actually found the instructions here to be the best for XmlPullParser: http://www.ibm.com/developerworks/opensource/library/x-android/index.html
NickAldwin
I thought I had upvoted you...oops!
NickAldwin