Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help to populate categories, questions and answers into SQLite for a quiz-app.

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!
I would like to make a QuizApp with 5 categories and a bunch of questions and 4 possible answers.
I would just like to know the best way of storing this data into the database. I already have a DBHelper class in my project that works for manually adding categories,question etc. But that's just for the users if
they would like to add there own questions to the app. I would like a way to store a bunch of default/start-questions in the code. Cant find any info on how or where to put that data in the code?
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not a good idea to decide to use a database before you decide what you want to store. Have you considered a question class with question and answers associated encapsulated? You can include wrong answers and randomise the order the answers appear in to make it less predictable.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It is not a good idea to decide to use a database before you decide what you want to store. Have you considered a question class with question and answers associated encapsulated? You can include wrong answers and randomise the order the answers appear in to make it less predictable.



Yea I do consider a question class. Maybe just to fill the DB. Should I just go something like this:?



DBHelper db = new DBHelper(this);

db.insertData("Music","What's the second best selling album?", "BACK IN BLACK");
db.insertData("Music","blablabla", "blabla");
db.insertData("Music","blablabla", "blablabla");
db.insertData("Music","blablabla", "blablabla");
db.insertData("Music","blablabla", "blablabla");



Is there a better way to do this by a loop? I cant really come up with any idea how that would save alot of space except the use of db.insertData every on every line. Maybe set the questions/answers as variables ->
q1,q2,q3 etc.. and loop trough with q + i (i++) ?? But that will just increase the amount of code I guess....
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Ungerfält wrote:. . . Should I just go something like this:?



DBHelper db = new DBHelper(this);

db.insertData("Music","What's the second best selling album?", "BACK IN BLACK");
db.insertData("Music","blablabla", "blabla");
db.insertData("Music","blablabla", "blablabla");
db.insertData("Music","blablabla", "blablabla");
db.insertData("Music","blablabla", "blablabla");
. . .
Probably not. That doesn't look like object‑oriented programming to me. You are not using question objects. I would think a question object would have several answers, and some indication of which the correct answer is. Just be very careful that you only have one correct answer.
 
Campbell Ritchie
Sheriff
Posts: 51368
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This talk about whether it is quicker to use a loop makes me suspect you are trying to work out how to do it before you have worked out what you want to do.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I managed to store some data with the use of a string for questions and string array for the answers no problem. But I want the answers to be stored in a hashmap<String,Boolean> But I got a nullpointerexception that way and cant figure out why Im affraid.

Here's the code :





MainActivity:


 
Carey Brown
Bartender
Posts: 1939
24
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When your problem manifests itself as a thrown exception, please make sure to cut and paste the stack trace in your post. And also make sure you have provided a listing of code that includes the line indicated by the stack trace.

Nit: You have a variable called 'list' which is not a list but an array. Better yet, name it for what the variable contains, e.g. 'answers', or something like that.

You have several loops with a return that is not enclosed in an if(), therefore, only the first element will ever be returned.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:When your problem manifests itself as a thrown exception, please make sure to cut and paste the stack trace in your post. And also make sure you have provided a listing of code that includes the line indicated by the stack trace.

Nit: You have a variable called 'list' which is not a list but an array. Better yet, name it for what the variable contains, e.g. 'answers', or something like that.

You have several loops with a return that is not enclosed in an if(), therefore, only the first element will ever be returned.



Yea the list was just a name i put there for a little experiment with array instead of hashmap, its works fine without curlybrackets actually.

Is this what you mean by stacktrace?:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.util.HashMap.put(java.lang.Object, java.lang.Object)' on a null object reference


Got two blue rows thats clickable in logcat which I guess causes trouble.


(Question.java:21) -> answers.put(correctAnswer, true);
MainActivity.java:35) -> db.insertData(new Question("WHAT BAND?","uiop","er","ty","qp"));




 
Liutauras Vilda
Bartender
Pie
Posts: 2786
112
BSD VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Why do you have all your fields as public? You shouldn't be doing that, make them private.

2. Variables a1, a2, a3, what these are? Answers.. You know, but someone else will need to read all your code to find out that.
Don't try to save space in the price of readability. Every variable has to tell you right away, what he is meant to store. We know about i and j because these are conventional for the loop iterations, but a1, a2 are not.

3. Code below. Apart from the missing curly braces around if statement block, formatting is also incorrect. for loop suppose to be shifted by 4 spaces to the right side. Again, everything goes down to the code readability, omitting the fact, that you could miss something important and that code could behave wrongly because of simply missing curly braces.
Actually there is something wrong with this code. Two mistakes I see. Method name suggests that it should return the list, but it appears it is returning not the list, but array element (single), not whole array. Also similar formatting problems in other method too.

That is it at the moment from me.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:1. Why do you have all your fields as public? You shouldn't be doing that, make them private.

2. Variables a1, a2, a3, what these are? Answers.. You know, but someone else will need to read all your code to find out that.
Don't try to save space in the price of readability. Every variable has to tell you right away, what he is meant to store. We know about i and j because these are conventional for the loop iterations, but a1, a2 are not.

3. Code below. Apart from the missing curly braces around if statement block, formatting is also incorrect. for loop suppose to be shifted by 4 spaces to the right side. Again, everything goes down to the code readability, omitting the fact, that you could miss something important and that code could behave wrongly because of simply missing curly braces.
Actually there is something wrong with this code. Two mistakes I see. Method name suggests that it should return the list, but it appears it is returning not the list, but array element (single), not whole array. Also similar formatting problems in other method too.

That is it at the moment from me.



Im very sorry for that :-)
I got a bad habbit on doing that just to clean it up later ;.)
Now again to the topic that is the hashmap. It returns this nullpointer as i mentioned above. I still got that same problem.

 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was just missing to set the Hashmap as a "new". Like hashmap = new Hashmap :-)

My only issue now is that my loop just returns one of the values. This is what shows when I view the database TOAST: Category: Music, Question: What is the best selling album? Correct Answer: BLACK IN BLACK Incorret Answer1 :BLACK IN BLACK IncorrectAnswer2 : BLACK IN BLACK Incorret Answer3 : BLACK IN BLACK
Got no errors in the log.

myDb.insertQuestions(new Question("MUSIC","What's the second best selling album?",
"NeverMind", "Thriller","BACK IN BLACK","Black Album"));



 
Dave Tolls
Ranch Hand
Posts: 2095
15
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is caused by what Carey said in the last line of his post.
You are looping over the Entries in the Map and returning the first one only.

You need to decide whether getAnswers returns a pre-formatted String you can simply display to the user, or whether it returns the keySet (you can get the keys by themselves from a map without the values) and leaves it up to the calling code to do the layout. I would prefer the latter.
 
Daniel Ungerfält
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That is caused by what Carey said in the last line of his post.
You are looping over the Entries in the Map and returning the first one only.

You need to decide whether getAnswers returns a pre-formatted String you can simply display to the user, or whether it returns the keySet (you can get the keys by themselves from a map without the values) and leaves it up to the calling code to do the layout. I would prefer the latter.


"You are looping over the Entries in the Map and returning the first one only."
Yes I noticed that. I just dont know how to solve that Im affraid.

"or whether it returns the keySet "
But thats just what Im trying to do? ---> return (String) entry. ---> getKey(); <---- (return (String) entry.getKey();)
 
Dave Tolls
Ranch Hand
Posts: 2095
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
keySet().

As for how to display it correctly, where is the code that calls getAnswers?
I presume there's a loop there.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic