2012-04-06 3 views
1

Ich habe zwei Tabellen "TABLE_EXAM" und "TABLE_QUESTION". Ich hole Datensatz mit folgendem Code
, aber es zeigt nur einen Datensatz. Ich muss alle Rekord aber einzeln nach dem Klicken auf Nächste Knopf zeigen. Bitte geben Sie mir einige Hinweise oder Hinweise.
Ich verstehe nicht, wie man Datensatz eins nach dem anderen abruft, indem ich auf Next Button klicke.
Vielen Dank im Voraus.Wie man einen Datensatz von sqlite einzeln holt?

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

, wie das zu tun?

Antwort

1

Schließlich Antwort, die ich bekam ... Ich zähle die Anzahl von Aufzeichnungs und es nur um 1 dekrementieren Wenn ich 10 Datensatz haben, dann setzen i-Zähler = 10 und dekrementieren Sie es bei jedem weiteren Klick einfach um 1.
Hier ist einfache Funktion, die mir helfen.

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

Auf Knopf klicken Sie einfach auf cursor Position das ist es.

Cursor eingebaut hat Fähigkeit, seine Position zu bewegen. Sie haben moveNext(), movePrevious(), moveToPosition(), moveToFirst(), moveToLast(). Neben diesem hat es auch getCount(), die Gesamtzahl der Aufzeichnung gibt.

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

    } 
}); 
2

Dies ist der Code, den Sie in Ihrer Java-Datei zu schreiben.

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()); 

        } 
      } 
} 

Dies ist die Funktion der Datenbank

public Cursor getDefault() throws SQLException { 

    String getRT = "SELECT * from "+ PROFILE_TABLE+";"; 
    Cursor mCur = sqldb.rawQuery(getRT, null); 
    return mCur; 
} 
1

erste Feuer Abfrage wie

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

Und jetzt Ende Empfang erhalten wie

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(); 

     } 

einen Zähler wie

initialisieren
int count = 0 ; 

und auf die Schaltfläche klicken Anzeige wie

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

} 
}