2014-10-08 170 views
0

在我的Android應用程序中,我需要在循環中顯示10個MCQs問題。我從sqlite數據庫中獲取問題。因此,我將限制sql查詢的問題轉換爲10個結果。但是第一次當我點擊按鈕顯示一個問題與MCQ(我用4個答案來選擇用戶)成功。然後當我第二次點擊按鈕它不工作。那是什麼問題?以下是我的代碼。按鈕按下循環

nextButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Button nextButton = (Button) findViewById(R.id.button1); 
      DataBaseHelper myDbHelper = new DataBaseHelper(MainActivity.this); 
      try { 
       myDbHelper.createDataBase(); 
      } catch (IOException ioe) { 
       throw new Error("Unable to connect to database"); 
      } 

      try { 
       myDbHelper.openDataBase(); 
       Cursor c = myDbHelper.retriveQuestionsData(); 

       setContentView(R.layout.quiz_home); 
       Log.d(Logcat, "Clicked"); 
       nextButton.setText("Next"); 

       String id = ""; 
       String question = ""; 
       int counter = 0; 

       int cursorsize = c.getCount(); 
       Log.d(Logcat, "Cursor size is: " + cursorsize); 

       if (c.moveToFirst()) { 
        do { 
         counter = counter + 1; 
         Log.d(Logcat, "Counter value is: " + counter); 
         id = c.getString(0); 
         question = c.getString(1); 
         TextView tvw = (TextView) findViewById(R.id.textView1); 
         tvw.setText("Q" + counter + ":" + question + "?"); 
         Cursor ansc = myDbHelper.retriveAnswersData(id); 
         if (ansc.moveToFirst()) { 
          do { 
           RadioGroup rgp = (RadioGroup) findViewById(R.id.radioGroup1); 
           rgp.setVisibility(View.VISIBLE); 

           for (int i = 0; i < rgp.getChildCount(); i++) { 
            int index = i + 1; 
            ((RadioButton) rgp.getChildAt(i)).setText(ansc.getString(index)); 
           } 
           String correctans = ansc.getString(5); 
           Log.d(Logcat, "database correct answer:" + correctans); 

           int selected = rgp.getCheckedRadioButtonId(); 
           RadioButton rb = (RadioButton) findViewById(selected); 
           String user_input = rb.getText().toString(); 
           SQLiteDatabase tempdb = new TempDatabaseHelper(MainActivity.this).getWritableDatabase(); 
           ContentValues cv = new ContentValues(); 

           if (user_input.equals(correctans)) { 
            cv.put("input", "1"); 
            tempdb.insert("user_input", null, cv); 
            Log.d(Logcat, "Correct"); 
           } else { 
            cv.put("input", "0"); 
            tempdb.insert("user_input", null, cv); 
            Log.d(Logcat, "Wrong"); 
           } 
           Log.d(Logcat, "DATA INSERTED"); 
           tempdb.close(); 
           Log.d(Logcat, "DATABASE CLOSED"); 
           Log.d(Logcat, "USER INPUT IS:" + user_input); 
          } while (ansc.moveToNext()); 
         } 
        } while (c.moveToNext()); 
       } 
      } catch (SQLException sqle) { 
       throw sqle; 
      } 
      myDbHelper.close(); 
      //setContentView(R.layout.results); 
     } 
    }); 

我剛剛用了一個計數器來確定循環的運行次數。

+0

'setContentView(R.layout.quiz_home);'在一個按鈕點擊偵聽器?什麼... – EpicPandaForce 2014-10-08 09:48:43

+0

這是問題頁面的佈局文件。 – CodeCanyon 2014-10-08 09:50:56

回答

0

終於可以解決它。我得到了一個錯誤的按鈕id。所以現在它的工作。

Button nextButton = (Button) findViewById(R.id.button2);