0

I'm working on my Android application's initialization. I plan to have 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:

  1. Is this a good approach?
  2. 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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your fast answer. The second option seems a easier option for doing it. But it'd be perfect if were posible to refer the strings in compile avoiding runtime crashes. Is posible to do it?
How do you want to check at compile-time a value that will only be available at runtime ? No, it's not possible. But the method getIdentifier will return 0 if resource doesn't exist, it's a good runtime check to prevent crashes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.