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.
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.
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");
. . .
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.
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.
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.