I have two table “TABLE_EXAM” and “TABLE_QUESTION”. I fetch record using following code
but it show only one record. I need to show all record but one by one after clicking on Next button.Please give me some reference or hint.
I don’t understand how to fetch record one by one by clicking on Next Button.
Thanks in Advance.
AppearingExamActivity.java
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
examId=db.getExamId(profile);
final List<ObjectiveWiseQuestion> QuestionWiseProfile= db.getOneQuestion(examId);
for (final ObjectiveWiseQuestion cn : QuestionWiseProfile)
{
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
//db.close();
txtQuestion.setText(cn.getQuestion());
optionA.setText(cn.getOptionA());
optionB.setText(cn.getOptionB());
optionC.setText(cn.getOptionC());
optionD.setText(cn.getOptionD());
correctOption=cn.getCorrectOption();
}
}
btnNext.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
db=new MySQLiteHelper(getBaseContext());
db.getWritableDatabase();
owq.getCorrectAnswer();
owq.setExamId(examId);
//owq.getExamId();
owq.getQuestionId();
db.addResultDetails(owq);
db.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
});
MySQLiteHelper.java
public List<ObjectiveWiseQuestion> getOneQuestion(int examId)
{
// long index = 0;
List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
db = getReadableDatabase();
String selectQuery=("select * from question where exam_id ='"+ examId +"'");
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst())
{
do {
ObjectiveWiseQuestion owq= new ObjectiveWiseQuestion();
owq.setQuestionId(cursor.getInt(0));
owq.setExamId(cursor.getInt(1));
owq.setQuestion(cursor.getString(2));
owq.setOptionA(cursor.getString(3));
owq.setOptionB(cursor.getString(4));
owq.setOptionC(cursor.getString(5));
owq.setOptionD(cursor.getString(6));
owq.setCorrectOption(cursor.getString(7));
LocwiseProfileList.add(owq);
} while(cursor.moveToNext());
db.close();
}
return LocwiseProfileList;
}
how to do that?
Finally I got answer… I count number of record and only decrement it by 1. If i have 10 record then i set counter=10 and simply decrement it by 1 on each next click.
Here is simple function which help me.