-1

I am developing quiz app on Android. I have asked someone about storing data. All people prefer Sqlite database. I did. But Why can't I use String data to display data, not Sqlite database? Example for more detail:

  • Store in List:
List<String> questions = new ArrayList<String>();
questions.add("Question A?");
questions.add("Question B?");
questions.add("Question C?");
List<String> answers = new ArrayList<String>();
answers.add("Answer A");
answers.add("Answer B");
answers.add("Answer C");
  • Store in Sqlite
Question question0 = new Question("Question A?", "Answer A");
Question question1 = new Question("Question B?", "Answer B");
Question question2 = new Question("Question C?", "Answer C");
dbHelper.createQuestion(question0);
dbHelper.createQuestion(question1);
dbHelper.createQuestion(question2);
List<String> listQuestions = new ArrayList<String>();
listQuestions = dbHelper.getAllQuestions();
kidsoul
  • 67
  • 1
  • 10

1 Answers1

1

For you understanding you should know that a database my contain anything from a String to image, audio, Videos etc. So when you fetch data you need a cursor so in that you get data from database and use in your layout by casting Accordingly. But if you would use only String then it would not be possible to show all the data and perform operations.

Pitty
  • 1,689
  • 4
  • 15
  • 33
Akshat Vajpayee
  • 288
  • 1
  • 3
  • 15
  • Thank for you answer. In my case, I just use text – kidsoul Mar 16 '16 at 08:55
  • 1
    One more important thing is that storing the data in the List in your case would not solve the purpose of your application. Because the data stored in the list or String variable is temporary if you close your app then all the data will be erased but using database you can store the data forever. – Akshat Vajpayee Mar 16 '16 at 09:11
  • thank you. i am newbie. i will remember that. But i have another issue. The first time run app, database is created on onCreate() method. So, it's again on second time run app. How can I optimize it ( just create once ) ? – kidsoul Mar 16 '16 at 09:22
  • onCreate() method will create the Db only for the first time when you did,nt create the db. So you dont have to worry about the optimization because next time it is used for getWriteableDatabase(); which is needed to get the reference of database. – Akshat Vajpayee Mar 16 '16 at 09:45
  • for another: i found detail explain here: http://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run – kidsoul Mar 16 '16 at 13:58