Can I use values from R.strings in a custom XML file?
I'm working on my Android application's initialization. I plan to hav开发者_如何学Pythone an XML
file in the assets folder with a format like this:
<?xml version="1.0" encoding="utf-16"?>
<Videos>
<Video>
<id>1</id>
<title>Funny Cat</title>
<description>Description for the video</description>
<url>http://......</url>
</Video>
<Video>
.......
</Video>
</Videos>
With a subclass of DefaultHandler
, I will parse the XML
file when the app first launches, in order to initialize the database (if its empty).
Having provided this background information, I have two questions:
- Is this a good approach?
- How can I set my video titles and descriptions from strings stored in
res/values/strings.xml
?
Any code would be much appreciated!
Thanks in advance.
If you want to initialize some data, putting your xml file in assets folder can be a good idea.
If you want to create and fill a database at startup, you should consider storing a copy of the sqlite db file in the assets folder and copy it at startup as explained in this great piece of blog.
Also, you can you use this blog to lookup for some resources by name at runtime. It will allow you to use the name of string of your strings.xml file inside your xml data file and get the localized version of the string for the users locale.
Regards, Stéphane
精彩评论