显示下一个数据库条目

时间:2016-10-19 13:16:36

标签: java android sqlite

我正在编写我的第一个Android应用程序。

我正在尝试创建一个测验应用程序。我有一个存储在SQLite数据库中的问题,这些问题一个接一个地显示出来 用户选择其中一个答案(单选按钮)并点击“下一步”按钮'并显示下一个问题,等等。

以下代码显示我的活动文件一个接一个地显示每个问题,完美地工作

<小时/> ACEActivity (旧版,工作版)

public class ACEActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button butNext;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    DatabaseHelper db = new DatabaseHelper(this);

    quesList = db.getAllACEQuestions();
    currentQ = quesList.get(qid);
    txtQuestion = (TextView)findViewById(R.id.textView);
    rda = (RadioButton)findViewById(R.id.radio0);
    rdb = (RadioButton)findViewById(R.id.radio1);
    rdc = (RadioButton)findViewById(R.id.radio2);
    rdd = (RadioButton)findViewById(R.id.radio3);
    butNext = (Button)findViewById(R.id.nextButton);
    setQuestionView();

    butNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioGroup grp = (RadioGroup)findViewById(R.id.radioGroup);
            RadioButton answer = (RadioButton)findViewById(grp.getCheckedRadioButtonId());
            Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
            // If the correct answer was clicked display the next question
            if(currentQ.getANSWER().equals(answer.getText())) {
                currentQ = quesList.get(qid);
                setQuestionView();
            }
        }
    });
}

// Load the next question
private void setQuestionView() {
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdd.setText(currentQ.getOPTD());
    qid++;
    }
}

<小时/> 在我的这个活动的新版本中,如果点击了正确或错误的答案,我会加载另一个活动,以便给予用户即时反馈。

显示反馈活动后,我想返回此活动并显示下一个问题。

我试图通过将问题ID从反馈活动(ACECorrectActivity)传递给此活动(ACEActivity)而没有任何成功。


我是如何尝试解决此问题的:

ACEActivity (新版本,仅适用于第一个问题)

public class ACEActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button checkBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_quiz);

    // THIS PART IS NEW ================================
    // Get the intent
    Intent intent = getIntent();
    // Get the question id (if there are any extras)
    Bundle extras = intent.getExtras();
    if (extras != null) {
        int qid = extras.getInt("nextQuestionID");
    } else {
        int qid = 0;
    }
    // ==================================================

    DatabaseHelper db = new DatabaseHelper(this);

    quesList = db.getAllACEQuestions();
    currentQ = quesList.get(qid);
    txtQuestion = (TextView)findViewById(R.id.textView);
    rda = (RadioButton)findViewById(R.id.radio0);
    rdb = (RadioButton)findViewById(R.id.radio1);
    rdc = (RadioButton)findViewById(R.id.radio2);
    rdd = (RadioButton)findViewById(R.id.radio3);
    checkBtn = (Button) findViewById(R.id.checkButton);

    setQuestionView(qid);

    checkBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup);
            RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
            Log.d("yourans", currentQ.getANSWER() + " " + answer.getText());
            // THIS PART IS NEW AND WORKING FINE ================================
            // If the correct answer was clicked
            if (currentQ.getANSWER().equals(answer.getText())) {
                Intent intent = new Intent(ACEActivity.this, CorrectACEActivity.class);
                startActivity(intent);
            // If the wrong answer was clicked
            } else {
                Intent intent = new Intent(ACEActivity.this, FalseACEActivity.class);
                startActivity(intent);
            }
        }
    });
}

private void setQuestionView() {
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdd.setText(currentQ.getOPTD());
    qid++;
    }
}

ACECorrectActivity (加载反馈活动,选择正确答案并在ACEActivity中点击下一个按钮)

public class CorrectACEActivity extends Activity {
List<Question> quesList;
int score = 0;
int qid = 0;
Question currentQ;
TextView txtQuestion;
RadioButton rda, rdb, rdc, rdd;
Button nextBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_correct);

    DatabaseHelper db = new DatabaseHelper(this);

    quesList = db.getAllACEQuestions();
    currentQ = quesList.get(qid);
    txtQuestion = (TextView) findViewById(R.id.textView);
    rda = (RadioButton) findViewById(R.id.radio0);
    rdb = (RadioButton) findViewById(R.id.radio1);
    rdc = (RadioButton) findViewById(R.id.radio2);
    rdd = (RadioButton) findViewById(R.id.radio3);
    nextBtn = (Button) findViewById(R.id.nextButton);

    // Set colors according to correct answer
    rda.setBackgroundColor(Color.RED);
    rdb.setBackgroundColor(Color.RED);
    rdc.setBackgroundColor(Color.RED);
    rdd.setBackgroundColor(Color.RED);

    if(currentQ.getANSWER().equals(currentQ.getOPTA())) {
        rda.setBackgroundColor(Color.GREEN);
    } else if(currentQ.getANSWER().equals(currentQ.getOPTB())) {
        rdb.setBackgroundColor(Color.GREEN);
    } else if(currentQ.getANSWER().equals(currentQ.getOPTC())) {
        rdc.setBackgroundColor(Color.GREEN);
    } else if(currentQ.getANSWER().equals(currentQ.getOPTD())) {
        rdd.setBackgroundColor(Color.GREEN);
    }

    setQuestionView();

    // WHEN NEXT BUTTON IS CLICKED RETURN TO ACEActivity AND LOAD NEXT QUESTION
    nextBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CorrectACEActivity.this, ACEActivity.class);
            intent.putExtra("nextQuestionID", currentQ + 1);
            startActivity(intent);
        }
    });
}

private void setQuestionView() {
    txtQuestion.setText(currentQ.getQUESTION());
    rda.setText(currentQ.getOPTA());
    rdb.setText(currentQ.getOPTB());
    rdc.setText(currentQ.getOPTC());
    rdd.setText(currentQ.getOPTD());
    qid++;
    }
}


第一个问题非常有效。但是,在回答完第一个问题后,一旦我回到ACEActivity,我就会再次提出第一个问题。


正如您所看到的,我对此非常陌生,并且非常乐意为您提供任何帮助!谢谢!!

1 个答案:

答案 0 :(得分:1)

 intent.putExtra("nextQuestionID", currentQ + 1);

你在CorrectACEActivity中设置了多余的错误,不应该是这样吗?

 intent.putExtra("nextQuestionID", qid+ 1);