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...
According to the doc,
/xml
is the way to go.Providing Resources
xml/
Arbitrary XML files that can be read at run-time by callingVarious XML configuration files must be saved here, such as a searchable configuration.
Documentation for
getXML()
I also made a working example:
the XML structure:
the Java class to hold parsed data:
the parser itself:
and finally, calling the parser:
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.
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:
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!!
I have used Simple for xml parsing in the past. I think it has the least amount of code if you know what to expect in xml, which in your case you do.
http://simple.sourceforge.net/