Why this code is not working?

Put your problem here if it does not fit any of the other categories.

Why this code is not working?

Postby Nabukodonosor » Sat Mar 16, 2013 8:17 pm

Actually it works but not every time. Once in 5 or 6 times it simple does not work. Anyway, I have a quiz with question and 4 possible answers. Questions are from pre populated sqlite database imported in assets folder. I use collections shuffle to place answers randomly to buttons. Now, I have a button for 50/50 help, like i Who wants to become a millionaire, to delete two wrong answers. Here's my question method:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void nextQuestion() {
  2.  
  3.         TestAdapter mDbHelper = new TestAdapter(this);
  4.         mDbHelper.createDatabase();
  5.  
  6.         try{
  7.  
  8.             mDbHelper.open();
  9.  
  10.             Cursor c = mDbHelper.getTestData(generateWhereClause());
  11.             mAnsweredQuestions.add(c.getLong(0));
  12.  
  13.             List<Answer> labels = new ArrayList<Answer>();
  14.  
  15.             labels.add(new Answer(c.getString(2), true));
  16.             labels.add(new Answer(c.getString(3), false));
  17.             labels.add(new Answer(c.getString(4), false));
  18.             labels.add(new Answer(c.getString(5), false));
  19.  
  20.             Collections.shuffle(labels);
  21.  
  22.             tacanOdg = c.getString(2);
  23.  
  24.             if(brojacPogresnihOdgovora < 5){
  25.  
  26.  
  27.         question.setText(c.getString(1));
  28.  
  29.         bOdgovor1.setText(labels.get(0).option);
  30.         bOdgovor1.setTag(labels.get(0));
  31.         bOdgovor1.setOnClickListener(clickListener);
  32.  
  33.         bOdgovor2.setText(labels.get(1).option);
  34.         bOdgovor2.setTag(labels.get(1));
  35.         bOdgovor2.setOnClickListener(clickListener);
  36.  
  37.         bOdgovor3.setText(labels.get(2).option);
  38.         bOdgovor3.setTag(labels.get(2));
  39.         bOdgovor3.setOnClickListener(clickListener);
  40.  
  41.         bOdgovor4.setText(labels.get(3).option);
  42.         bOdgovor4.setTag(labels.get(3));
  43.         bOdgovor4.setOnClickListener(clickListener);
  44.  
  45.         netacniOdg.setText("" + brojacPogresnihOdgovora);
  46.         score.setText("Score: " + brojacTacnihOdgovora);
  47.             }
  48.  
  49.         }
  50.         finally{
  51.             mDbHelper.close();
  52.         }
Parsed in 0.037 seconds, using GeSHi 1.0.8.4



I use this code to dismiss two wrong answers:


Syntax: [ Download ] [ Hide ]
  1. bPolaPola.setOnClickListener(new OnClickListener() {  
  2. @Override 
  3. public void onClick(View arg0) { 
  4. int rnd = new Random().nextInt(3) + 1; 
  5. int i = 0; 
  6.  
  7. if (!((Answer)bOdgovor1.getTag()).isCorrect) { 
  8. i++; 
  9. if (i != rnd) { 
  10. bOdgovor1.setText(""); 
  11. if (!((Answer)bOdgovor2.getTag()).isCorrect) { 
  12. i++; 
  13. if (i != rnd) { 
  14. bOdgovor2.setText(""); 
  15. if (!((Answer)bOdgovor3.getTag()).isCorrect) { 
  16. i++; 
  17. if (i != rnd) { 
  18. bOdgovor3.setText(""); 
  19. if (!((Answer)bOdgovor4.getTag()).isCorrect) { 
  20. i++; 
  21. if (i != rnd) { 
  22. bOdgovor4.setText(""); 
  23. bPolaPola.setText("X"); 
  24. bPolaPola.setEnabled(false); 



But it's not working every time. Where's my mistake?
Nabukodonosor
Freshman
Freshman
 
Posts: 5
Joined: Fri Feb 22, 2013 1:56 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 19 guests