我正在研究QuizActivity并且无法从最后一个问题到达时从数据库获取上一个问题,然后点击上一个按钮然后第一个问题出现在活动中。最近2天需要帮助。感谢
DATABASE
public List<NotificationListItem> getNQuestions() {
List<NotificationListItem> quest = new ArrayList<NotificationListItem>();
String selectQuery = "SELECT * FROM " + TABLE_NAME2;
openToRead();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
NotificationListItem item = new NotificationListItem();
item.setqID(cursor.getInt(1));
item.setAnswer(cursor.getString(2));
item.setTestID(cursor.getString(3));
item.setquestions(cursor.getString(4));
item.setOption1(cursor.getString(5));
item.setOption2(cursor.getString(6));
item.setOption3(cursor.getString(7));
item.setOption4(cursor.getString(8));
item.setOption5(cursor.getString(9));
item.setAnscount(cursor.getString(10));
item.setTc(cursor.getString(11));
quest.add(item);
} while (cursor.moveToNext());
}
return quest;
}
public List<NotificationListItem> getPQuestions() {
List<NotificationListItem> quest = new ArrayList<NotificationListItem>();
String selectQuery = "SELECT * FROM " + TABLE_NAME2;
openToRead();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
NotificationListItem pitem = new NotificationListItem();
pitem.setqID(cursor.getInt(1));
pitem.setAnswer(cursor.getString(2));
pitem.setTestID(cursor.getString(3));
pitem.setquestions(cursor.getString(4));
pitem.setOption1(cursor.getString(5));
pitem.setOption2(cursor.getString(6));
pitem.setOption3(cursor.getString(7));
pitem.setOption4(cursor.getString(8));
pitem.setOption5(cursor.getString(9));
pitem.setAnscount(cursor.getString(10));
pitem.setTc(cursor.getString(11));
quest.add(pitem);
} while (cursor.moveToNext());
}
return quest;
}
public int getpreviousid() {
int previd=0;
openToRead();
String selectQuery = "SELECT * FROM " + TABLE_NAME2;
Cursor cursor=null;
cursor = db.rawQuery(selectQuery, null);
if (cursor != null && cursor.moveToPrevious()) {
previd=cursor.getInt(cursor.getColumnIndex(KEY_QID));
}
cursor.close();
return previd;
}
QUIZACTIVITY
NotificationListItem Nitem,Pitem;
List<NotificationListItem> Nquest,Pquest;
int score=0;
int qID=0,anscount,totalchoice,tq,questionid,previd;
QuizTable quizTable;
LinearLayout rc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_quiz_activity_b);
qs = getIntent().getStringExtra("questions");
tq = Integer.parseInt(qs);
prevBT.setEnabled(false);
quizTable = new QuizTable(this);
quizTable.openToRead();
quizTable.openToWrite();
Nquest=quizTable.getNQuestions();
Pquest=quizTable.getPQuestions();
lastid=quizTable.getLastId();
firstid=quizTable.getFirstId();
previd=quizTable.getpreviousid();
Nitem=Nquest.get(questionid);
Pitem=Pquest.get(previd);
setQuestionView();
nextBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(questionid<Integer.parseInt(lastid)){
Nitem=Nquest.get(questionid);
prevBT.setEnabled(true);
setQuestionView();
}
else{
Intent intent = new Intent(QuizActivityB.this, TestAnalysisActivity.class);
startActivity(intent);
finish();
}}
});
prevBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(questionid<Integer.parseInt(lastid)){
Pitem=Pquest.get(previd);
questionid=previd;
setQuestionView1();
}
if(questionid==Integer.parseInt(lastid))
{
Pitem=Pquest.get(previd);
questionid=previd;
setQuestionView1();
}
}
});
}
private void setQuestionView()
{
txtQuestion.setText(Nitem.getquestions());
op1=(Nitem.getOption1());
op2=(Nitem.getOption2());
op3=(Nitem.getOption3());
op4=(Nitem.getOption4());
op5=(Nitem.getOption5());
ansc=Nitem.getAnscount();
tc=(Nitem.getTc());
questionid=(Nitem.getqID());
if(questionid==Integer.parseInt(firstid))
{
prevBT.setEnabled(false);
}
totalchoice=Integer.parseInt(tc);
anscount=Integer.parseInt(ansc);
System.out.println("QUESTIONID="+questionid);
if(anscount==1)
{
rc.removeAllViews();
addRadioButtons();
}
else if(anscount>1)
{
rc.removeAllViews();
addCheckButtons();
}}
private void setQuestionView1() {
// TODO Auto-generated method stub
if(questionid==Integer.parseInt(firstid))
{
prevBT.setEnabled(false);
}
txtQuestion.setText(Pitem.getquestions());
op1=(Pitem.getOption1());
op2=(Pitem.getOption2());
op3=(Pitem.getOption3());
op4=(Pitem.getOption4());
op5=(Pitem.getOption5());
ansc=Pitem.getAnscount();
tc=(Pitem.getTc());
questionid=(Pitem.getqID());
totalchoice=Integer.parseInt(tc);
anscount=Integer.parseInt(ansc);
System.out.println("PREVIOUSID="+ questionid);
if(anscount==1)
{
rc.removeAllViews();
addRadioButtons();
}
else if(anscount>1)
{
rc.removeAllViews();
addCheckButtons();
}
}
答案 0 :(得分:0)
我们按下Next,这意味着显然增加了questionID:
nextBT.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
questionid++; // MISSING IN UR CODE
if(questionid<Integer.parseInt(lastid)){
Nitem=Nquest.get(questionid);
prevBT.setEnabled(true);
setQuestionView();
}
else{
Intent intent = new Intent(QuizActivityB.this, TestAnalysisActivity.class);
startActivity(intent);
finish();
}}
});