android sqlite下一条记录不正确

时间:2012-09-24 21:39:20

标签: android sqlite

这是getContact方法,在这种情况下将rowID作为计数器

public Cursor getContact(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                        question, possibleAnsOne,possibleAnsTwo, possibleAnsThree,realQuestion,userR}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            // moves to the the first record
            mCursor.moveToFirst();
        }
        return mCursor;
    }

这是我的问题。我从第一个开始。所以计数器设置为1。

在public oncreate方法中。当我因某种原因点击下一步时,它没有用db.updateUserResult设置第一条记录的值(counter,8);我认为它缺少了第一张唱片。我知道这一点的原因是,如果我选择了前面的答案。让我们说问题1正确答案是选项2而问题2的正确答案是选项3.如果您为问题1选择选项3,那么您将得到正确的答案。如果我继续选择正确的答案,我会得到所有正确的结果。

 public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);



            Bundle b = getIntent().getExtras(); 
            boolean TimerStarted = b.getBoolean("timerlogic");
            System.out.println("what is the logic of the timer" + TimerStarted);
            Log.e(LOGS, "Whatis the first value for the timer inside the timer_btn" + TimerStarted);

            setContentView(R.layout.basic);
            // this method is better then a try and catch block as it actually prevents the occurence occuring instead of just patching it.
            // the purpose of t
            db.open();

            Cursor c = db.getContact(1); 

            if(c.isFirst())
            {
                TextView question = (TextView)findViewById(R.id.question);
                RadioButton radio1 = (RadioButton)findViewById(R.id.radio1);
                RadioButton radio2 = (RadioButton)findViewById(R.id.radio2);
                RadioButton radio3 = (RadioButton)findViewById(R.id.radio3);





                answerCounterText = (TextView)findViewById(R.id.answerCounterText);
                answerCounterText.setText(String.valueOf("0")); 
                // for the first record set the counter to zero
                // question.setText(String.valueOf("0"));
                TextView QuestionNumber = (TextView)findViewById(R.id.QuestionNumber);
                Complete = (Button)findViewById(R.id.Continue);
                Complete.setBackgroundResource(R.drawable.complete);
                Complete.setVisibility(View.INVISIBLE);
                QuestionNumber.setText(String.valueOf("Question Number  :" + counter));

                  System.out.println("what is the setup counter after loop" + counter);




                //DisplayContact(c,radio1);
                DisplayRadioButton(c,radio1,radio2,radio3,question);

            }


          // }
          //  }
         // Previous = (Button)findViewById(R.id.Previous);
           Next = (Button)findViewById(R.id.Next);

           Next.setBackgroundResource(R.drawable.next);
           // get the results from the checked box


           Next.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                // Cursor c = db.getContact(1);

                /*
                if (firstrecordchosen)
                {
                    Cursor c = db.getContact(1);
                      System.out.println("what is the current value of the counter 0" + counter);
                    firstrecordchosen = false;
                }
                counter++;

                  */   
                counter++;
                 Cursor c = db.getContact(counter);

                    if (c.moveToFirst() && !c.isNull(0))        
                    {    
                         TextView question = (TextView)findViewById(R.id.question);
                        RadioButton radio1 = (RadioButton)findViewById(R.id.radio1);
                         RadioButton radio2 = (RadioButton)findViewById(R.id.radio2);
                         RadioButton radio3 = (RadioButton)findViewById(R.id.radio3);



                      TextView answerCounterText = (TextView)findViewById(R.id.answerCounterText);
                         TextView QuestionNumber = (TextView)findViewById(R.id.QuestionNumber);
                         RadioGroup radioGroup1 = (RadioGroup)findViewById(R.id.radioGroup1);

                         QuestionNumber.setText(String.valueOf("Question Number  :" + counter));
                         Log.e(LOGS, "default user set variable " + c.getString(5));
                         Log.e(LOGS, "real value" + c.getString(6));
                         System.out.println("what is the current value of the counter" + counter);




                    //  DisplayContact(c,radio1);
                   DisplayRadioButton(c,radio1,radio2,radio3,question);
                  final String questionOneDb = c.getString(5);
                  String radioOneIndex = "1";
                  String radioTwoIndex = "2";
                  String radioThreeIndex = "3";



                  // set the first question to have the right value

                  if(radio1.isChecked() )
                    {     
                        db.updateUserResult(counter,8);
                        if (questionOneDb.equals(radioOneIndex))
                        {

                            Log.e(LOGS, "correct" );    


                        rightAnswer(c,radio1,answerCounterText);
                        //  DisplayContact(c,radio1);
                        }
                        else
                        {

                            Log.e(LOGS, "wrong" );  
                        }


                    }




                    if(radio2.isChecked()  )
                    {

                        db.updateUserResult(counter,9);

                        if (questionOneDb.equals(radioTwoIndex))
                        {
                          System.out.println("is this counter being reached"); 
                        //  db.updateUserResult(1, 2);
                        //  db.updateUserResult(counter, 2);
                        // db.updateUserResult(recordval, 6);

                            Log.e(LOGS, "correct" );    


                        rightAnswer(c,radio1,answerCounterText);
                        }
                        else
                        {
                            Log.e(LOGS, "wrong" );      
                        }

                    }



                    if(radio3.isChecked()   )
                    { 
                        db.updateUserResult(counter,10);

                        if (questionOneDb.equals(radioThreeIndex))
                        {
                            Log.e(LOGS, "correct" );    
                        System.out.println("you have the right answer");
                        rightAnswer(c,radio1,answerCounterText);
                        }
                        else
                        {
                            Log.e(LOGS, "wrong" );      
                        }


                    }








                    }



                  /* when you get ot the last record you want to be able to close the db */
                    if (c.isAfterLast())

                    { 

                    // this halts the timer when there are no questiosn left
                   Next.setVisibility(View.GONE);
               //    Previous.setVisibility(View.GONE);
                   Toast.makeText(getBaseContext(), "More questions available in the full version PHPExpert " , Toast.LENGTH_LONG).show();
                   Complete.setVisibility(View.VISIBLE);
                    handler.removeCallbacks(timedTask);
                    db.close();

                  intentcalls();


                    }






            }
        });

           Home = (Button)findViewById(R.id.home_btn);
           Home.setOnClickListener(new View.OnClickListener() {

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

                Intent i = new Intent(Basic_database_questionsActivity.this,AndroidGUIActivity.class);
                startActivity(i);


            }


           }); 



        }

2 个答案:

答案 0 :(得分:1)

如果我不是完全错了,你就有时间问题。

首先,你得到第一个问题并显示用户界面(你的单选按钮和东西)。

如果选择的答案是正确的,请点击“下一步”按钮进行检查,是否正确?

如果是这样,你犯的错误是你先增加计数器,然后根据计数器查询问题(这将是下一个)。之后,检查所选的单选按钮是否正确。当您已经从数据库中读到下一个问题时,您将使用第二个问题的存储答案检查所选答案。

希望很清楚。

简短提示:尝试清理一些代码。不要在onCreate()内部放置任何小块,以便您可以更轻松地查看和遵循代码流...

答案 1 :(得分:0)

试试这个

Cursor c1 = mDbHelper.fetAll(); \\ or whatever your method is to get data from database.
if(c1.moveToFirst()){
     do{
        \\ Do what you want to do with it.

       }while(c1.moveToNext()); \\ this will move the cursor to next line.
    }
 c1.close(); \\ make sure you close it, else errors will pop up in LOGCAT.