1

我有两个表“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;
}

怎么做?

4

4 回答 4

3

在按钮上单击只是处理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();
        }

    }
});
于 2012-04-06T11:39:49.123 回答
2

这是您必须在 java 文件中编写的代码。

Cursor mCursor=null;
mCursor = DatabaseObject.getDefault(); // 
if (mCursor.getCount() >= 1) {
                mCursor.moveToFirst();
                try {
                    for (int i = 0; i < mCursor.getCount(); i++) {
                        profileName.setText(mCursor.getString(0).toString()); // You can Do your work Here 
                        firstName.setText(mCursor.getString(1).toString());
                        lastName.setText(mCursor.getString(2).toString());
                        email.setText(mCursor.getString(3).toString());

                     }
            }
}

这是数据库的功能

public Cursor getDefault() throws SQLException {

    String getRT = "SELECT * from "+ PROFILE_TABLE+";";
    Cursor mCur = sqldb.rawQuery(getRT, null);
    return mCur;
}
于 2012-04-06T11:45:55.017 回答
1

第一次触发查询

public Cursor Value_Of_Data(String string) throws SQLException {
        // TODO Auto-generated method stub


         Cursor mCursor = db.query(Normal_Scoring,
                  new String[] {
Columns1,Columns2,Columns3,Columns4,Columns5,Columns1
},
                  Game_Date +"<='" + string + "'",
                  null,null,null,null,null);





        if (mCursor != null)
        {
        mCursor.moveToFirst();
        }
        return mCursor;
    }

现在接收端收到喜欢

  ArrayList<String> row1 = new ArrayList<String>();

          ArrayList<String> row2 = new ArrayList<String>();

          ArrayList<String> row3 = new ArrayList<String>();

          ArrayList<String> row4 = new ArrayList<String>();

          ArrayList<String> row5 = new ArrayList<String>();

          ArrayList<String> row6 = new ArrayList<String>();




    try{
            cursor1 = db.Nomal_Score_Data(dateFormat.format(dateis));
            cursor1.moveToFirst();   
            startManagingCursor(cursor1);

            for(int i=0;i<cursor1.getCount();i++){


            String rec1 = cursor1.getString(cursor1.getColumnIndex
                                ("Columns1"));



            String rec2 = cursor1.getString(cursor1.getColumnIndex
                ("Columns2"));



            String rec3 = cursor1.getString(cursor1.getColumnIndex
                ("Columns3"));



            String rec4 = cursor1.getString(cursor1.getColumnIndex
                ("Columns4"));


            String rec5 = cursor1.getString(cursor1.getColumnIndex
                    ("Columns5"));




            String rec6 = cursor1.getString(cursor1.getColumnIndex
                       ("Columns6"));


          row1.add(rec1);  

         row12.add(rec2);

          row3.add(rec3);

         row4.add(rec4);

         row5.add(rec5);

          row6.add(rec6);

}
        catch(Exception e)
        {
            Log.e("ERROR OF FETCHING ", e.toString());
            e.printStackTrace();

        }

初始化一个计数器

int count = 0 ;

和按钮点击显示像

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub



        switch(v.getId())
        {
        case R.id.goNext :



            // display


       text1.setText(row1.get(count));
       text2.setText(row2.get(count));
       text3.setText(row3.get(count));
       text4.setText(row4.get(count));
       text5.setText(row5.get(count));
       text6.setText(row6.get(count));

   count++;


             }

                     break; 

case R.id.goPrev :

            Toast.makeText(this, "Button Clicked goPrev",
                    Toast.LENGTH_LONG).show();



       text1.setText(row1.get(count));
       text2.setText(row2.get(count));
       text3.setText(row3.get(count));
       text4.setText(row4.get(count));
       text5.setText(row5.get(count));
       text6.setText(row6.get(count));


         count--;



                     break; 

}
}
于 2012-04-06T12:43:23.163 回答
1

最后我得到了答案...我计算记录数,然后只减 1。如果我有 10 条记录,那么我设置 counter=10 并在每次下次单击时将其减 1。
这是对我有帮助的简单功能。

public List<ObjectiveWiseQuestion> getOneByOneQuestion(int examId, int questionId)
{      
    List<ObjectiveWiseQuestion>LocwiseProfileList=new ArrayList<ObjectiveWiseQuestion>();
     db = getWritableDatabase(); 

     String selectQuery=("select * from question where exam_id ='" + examId + "' and question_id ='" + questionId +"'" );
        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());
        cursor.moveToFirst();
        db.close();

    }


    return LocwiseProfileList;
}
于 2012-04-21T09:09:42.197 回答