android - 如何从sqlite中一条一条地获取记录?

标签 android sqlite record fetch

我有两个表“TABLE_EXAM”和“TABLE_QUESTION”。我使用以下代码获取记录
但它只显示一条记录。单击下一步按钮后,我需要一条一条地显示所有记录。请给我一些引用或提示。
我不明白如何通过单击“下一步”按钮来一条一条地获取记录。
提前致谢。

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

怎么做?

最佳答案

在按钮上单击只需处理 cursor 位置即可。

内置光标可以移动其位置。 你有 moveNext()movePrevious()moveToPosition()moveToFirst()moveToLast ()。除此之外,它还有 getCount(),它给出记录总数。

http://developer.android.com/reference/android/database/Cursor.html

String selectQuery=("select * from question where exam_id ='"+ examId +"'");
        Cursor cursor = db.rawQuery(selectQuery, null);

btnNext.setOnClickListener(new View.OnClickListener()
    {           
        @Override
        public void onClick(View v)
        {
            try
            {
               cursor.moveNext();
               //show on UI with cursor value
           }

        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
});

关于android - 如何从sqlite中一条一条地获取记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10042905/

相关文章:

java - 表 Entry_table 没有名为 DATE 的列(代码 1)

android - 使用 Room 加入查询

arrays - 包含带有 'Implicit' 类运算符的动态数组的 Delphi Record 的初始化

record - 有没有更方便的方法来使用嵌套记录?

android - Android google io 2011 java源代码中的MAGIC number

android - 如何比较android中的开始时间和结束时间?

android - Kotlin Gson 反序列化

插入 sqlite3 值时出现 Python TypeError?

record - 从集合更新记录字段

android - 将 AutoCompleteTextView 限制为仅一个结果