我正在構建一個應用程序,它需要一個後退和下一個按鈕來顯示從數據庫中檢索到的記錄。我已經將這些記錄加載到了一個arrayList中,現在我正面臨着如何使記錄在按鈕單擊時來回切換的兩難困境,即如何操作列表中的記錄。請幫忙。如何在記錄之間來回加載到arraylist上
List<Question> questionList = new ArrayList<Question>();
Cursor cursor = myDataBase.rawQuery(selectQuery, null);
if(cursor != null && cursor.getCount()>0){
if(cursor.moveToFirst()){
//do a loop to get all the questions
do{
Question quest = new Question();// create a new Question object
quest.setID(cursor.getInt(0));//get the question id for the cursor and set it to the object
quest.setQUESTION(cursor.getString(1));//same here for the question and the answers
quest.setCorrectAnswer(cursor.getString(2));
questionList.add(quest);//finally add the question to the list
}while(cursor.moveToNext());//move to next question until you finish with all of them
}
}
else {
Toast.makeText(myContext, "No records yet!", Toast.LENGTH_SHORT).show();
}
這是我的我的記錄加載到ArrayList中,從我的活動我做了loadPrevious和loadNext方法去來回按一下按鈕,我也做了loadNext部分和它的作品,我只是不不知道如何實現loadPrevious按鈕。
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//currentQ.setChoice(num);
loadNextQuestion();
}
});
protected void loadNextQuestion() {
// TODO Auto-generated method stub
Log.e(TAG,"load next question original");
if (questions.size() > 2)
{
currentQ = questions.remove(0);
}
question.setText(currentQ.getQUESTION());
correctAnswer = currentQ.getCorrectAnswer();
answer.setText(correctAnswer);
}
和問題列表看起來像這樣
package com.threeleafsoln.questionanswer;
public class Question {
private int ID;
private String QUESTION;
private String ANSWER;
public Question()
{
ID=0;
QUESTION="";
ANSWER="";
}
public Question(String qUESTION, String cORRECT_ANSWER) {
QUESTION = qUESTION;
ANSWER = cORRECT_ANSWER;
}
public int getID()
{
return ID;
}
public String getQUESTION() {
return QUESTION;
}
public String getCorrectAnswer() {
return ANSWER;
}
public void setID(int id)
{
ID=id;
}
public void setQUESTION(String qUESTION) {
QUESTION = qUESTION;
}
public void setCorrectAnswer(String aNSWER) {
ANSWER = aNSWER;
}
}
什麼我不明白這裏是怎麼了櫃檯,我questionList相關的,我的意思是給我我應該如何在索引計數器加載問題的線索+ 1 – sapamlucy 2014-12-11 06:55:43
請參閱編輯的函數loadNextQuestion和loadPreviousQuestion。 @sapamlucy – 2014-12-11 06:58:23
我已經實現了你的邏輯和前一個按鈕根本沒有反應,下一個按鈕工作正常,但有什麼建議嗎? – sapamlucy 2014-12-11 07:03:19