Dwebtron
Dwebtron

Reputation: 816

What is the best way to pre-load data with the application?

So, I have an app that I want to store a large series of Strings with, and I want it to ship with the application. In Android's onCreate method, should I just have a huge (couple of hundred lines) String[] initializer? Is there a better way to do this?

I was thinking of creating my own object of these (they are grouped up a little bit) and storing it that way, but that still includes an instantiation of lots of Strings into an array. Is there some way that I can have this already created? Is there a better way to do this?

Upvotes: 1

Views: 696

Answers (2)

lowcrawler
lowcrawler

Reputation: 7589

If it's only a couple hundred lines, I wouldn't concern myself too much. You can either 'hard code' them, creating a few hundred strings, or have them in a secondary 'flat file' that gets read in by the constructor. Either way, the result, is a few hundred Strings...

For speed - hard code them.

This previous question has a bit of discussion on the 'how' to do it for separate objects... how do i preload a hashmap in an object(without put method)?

Upvotes: 0

user1282637
user1282637

Reputation: 1867

You can do it in the background with an AsyncTask (separate thread). Like this:

private class InitializeString extends AsyncTask<Void, Void, Void> {

    @Override
    protected void doInBackground(Void... params) {

        //do your initializing

    }

    @Override
    protected void onPostExecute(Void... params) {}

    @Override
    protected void onPreExecute() {}

    @Override
    protected void onProgressUpdate(Void... params) {}
}

Also, do names matter? You can just create an ArrayList of strings like this:

ArrayList<String> myListOfStrings = new ArrayList<String>();
myListOfStrings.add("String 1");
myListOfStrings.add("String 2");
  ...

If the string values are hardcoded and aren't added dynamically, you could also add them all to strings.xml file.

Upvotes: 1

Related Questions