I am building an app which requires a back and next button to show records retrieved from a database. i have loaded the records in an arrayList and now I am facing the dilemma of how to make my records switch back and forth on button click, i.e how to manipulate the records in the list. Please help.
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();
}
this is how I load my records into the arrayList, from my activity i do a loadPrevious and loadNext method to go back and forth on button click, I have done the loadNext part and it works, I just don't know how to implement the loadPrevious button.
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);
}
and the Question list looks like this
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;
}
}
You just need to maintain the counter as per which record is being shown currently.
Firstly, when you show 1st ever question from arraylist, initialize that counter to question number. (assume its 1st question = 0th element of ArrayList)
So, it will be:
qCounter = 0;
Now increase counter by 1 in Next's onClick and decrease counter by 1 in Previous's 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);
}
Hope this helps.