按钮按下循环

时间:2014-10-08 09:44:55

标签: android

在我的Android应用程序中,我需要在循环中显示MCQ的10个问题。我从sqlite datbase获取问题。所以我得到的问题是将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);
        }
    });

我刚使用计数器来识别循环的运行次数。

1 个答案:

答案 0 :(得分:0)

最后可以解决它。我的按钮ID错了。现在它可以工作了。

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