Store static data in Android - custom resource?

NickAldwin picture NickAldwin · Jul 7, 2010 · Viewed 31.1k times · Source

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...

Answer

Paresh Mayani picture Paresh Mayani · Jul 8, 2010

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!!