Android : how to go back and forth between records loaded onto an arraylist

on Wednesday, December 10, 2014


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. Pl. any1 help and thanks in advance.



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 dont 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);
}

0 comments:

Post a Comment