2014-12-11 28 views
0

我正在構建一個應用程序,它需要一個後退和下一個按鈕來顯示從數據庫中檢索到的記錄。我已經將這些記錄加載到了一個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; 
} 


} 

回答

1

你只需要保持按了當前正在顯示記錄的計數器。

首先,當您從arraylist中顯示第一個問題時,將該計數器初始化爲問題編號。 (假設其第一次的問題= ArrayList中的第0個元素)

所以,這將是:

qCounter = 0; 

在接下來的onClick現在增加計數器1和1上的onClick減少計數器。

next.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     if(qCounter < questions.size()){ //i.e. its not last question of list 
      loadNextQuestion(); 
      qCounter++; 
     } 
    } 
}); 

prev.setOnClickListener(new View.OnClickListener() { 

    @Override 
    public void onClick(View v) { 
     if(qCounter > 0){ //i.e. its not first question of list 
      loadPreviousQuestion(); 
      qCounter--; 
     } 
    } 
}); 

protected void loadNextQuestion() { 
    //show item with index = (qCounter + 1) 
    currentQ = questions.get(qCounter + 1); 
    question.setText(currentQ.getQUESTION()); 
    correctAnswer = currentQ.getCorrectAnswer(); 
    answer.setText(correctAnswer); 
} 

protected void loadPreviousQuestion() { 
    //show item with index = (qCounter - 1) 
    currentQ = questions.get(qCounter + -1); 
    question.setText(currentQ.getQUESTION()); 
    correctAnswer = currentQ.getCorrectAnswer(); 
    answer.setText(correctAnswer); 
} 

希望這會有所幫助。

+0

什麼我不明白這裏是怎麼了櫃檯,我questionList相關的,我的意思是給我我應該如何在索引計數器加載問題的線索+ 1 – sapamlucy 2014-12-11 06:55:43

+0

請參閱編輯的函數loadNextQuestion和loadPreviousQuestion。 @sapamlucy – 2014-12-11 06:58:23

+0

我已經實現了你的邏輯和前一個按鈕根本沒有反應,下一個按鈕工作正常,但有什麼建議嗎? – sapamlucy 2014-12-11 07:03:19

0

允許程序員在任一方向遍歷列表的列表迭代器。不要從ArrayList中刪除問題,你可以使用索引來引用它們。

請參閱本link爲例

ListIterator<Question> itr=questionList.listIterator(); 

protected void loadNextQuestion() { 
    if (itr.hasNext){ 
    currentQ = itr.next(); 
    question.setText(currentQ.getQUESTION()); 
    correctAnswer = currentQ.getCorrectAnswer(); 
    answer.setText(correctAnswer); 
    } 
} 

protected void loadPreviousQuestion() { 
    if (itr.hasPrevious){ 
    currentQ = itr.previous(); 
    question.setText(currentQ.getQUESTION()); 
    correctAnswer = currentQ.getCorrectAnswer(); 
    answer.setText(correctAnswer); 
    } 
} 
+0

如何使用我的當前程序形式與Question類的迭代器。給我一個提示 – sapamlucy 2014-12-11 07:32:45

+0

用示例代碼更新了我的答案。希望有所幫助 – Shiv 2014-12-11 09:18:33

相關問題