Are all the strings from a program (in .java) stored in RAM when it's launched (Android)?
I'm writing a game on Android similar to Who wants to be a millionaire, and I'm wondering what s the best way to store the questions and answers. I want to have at least a 300-400 questions to start with and add about 100 every update. Right now I have them stored in a static class like this:
public class Qs {
public static Question questions[][];
public static Question q1e1,q1e2,q1e3,q2e1,q2e2,q2e3,q3e1,q3e2,q3e3,q4e1,q4e2,q4e3;
public static void load(){
q1e1 = new Question("Question","Answer","wrong answer","wrong answer","wrong answer");
q1e2 = new Question("Question","Answer","wrong answer","wrong answer","wrong answer");
q1e3 = new Question("Question","Answer","wrong answer","wrong answer","wrong answer");
Question level1[] = new Question[]{q1e1,q1e2,q1e3};
Question level2[] = new Question[]{q2e1,q2e2,q2e3};
Question level3[] = new Question[]{q3e1,q3e2,q3e3};
Question level4[] = new Question[]{q4e1,q4e2,q4e3};
questions = new Question[][]{level1,level2,level3,level4};
}
}
But I really don't know what will this do when I have hundreds of questions in that clas. Will they all load into memory and probably slow the phone down a lot ? For editing and adding questions it's fine, no need for fancy xml or anything, I'm just concerned about slowing down the开发者_如何学运维 phone (otherwise my app is really not demanding). Thanks for any answers, comments and tips!
Use classes for logic, not storage. You can either use SharedPreferences (doesn't fit well) or SQLite (It is meant for such usage).
You can go with Sqlite as a persistence store and take a little piece out in the memory when needed.
You should consider using an external storage for this.
This will make you application smaller. This will also ease the maintenance and allow you maintaining the questionnaire outside the app: a new version of the questionnaire will not lead you to recompile and republish the app (you can make your app to download new versions of the questionnaire from time to time, but this is another topic).
At first glance I see two valid options:
SQLite (lightweight database embedded in Android)
Android external storage (file system that allows you storing XMLs, or keys/values text files, or any other format)
There is an interesting post about that on the Android blog: http://developer.android.com/guide/topics/data/data-storage.html
精彩评论