2016-11-07 15 views
1

我是Android Studio和Java的初學者,我幾乎完成了我的第一個Android應用程序,但是,我試圖添加5個以上的問題,但是當我添加問題6它沒有出現。下面是從我DbHelper.java類的代碼:在Android遊戲中添加一個問題

package com.example.triviality; 
import java.util.ArrayList; 
import java.util.List; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
public class DbHelper extends SQLiteOpenHelper { 
    private static final int DATABASE_VERSION = 1; 
    // Database Name 
    private static final String DATABASE_NAME = "triviaQuiz"; 
    // tasks table name 
    private static final String TABLE_QUEST = "quest"; 
    // tasks Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_QUES = "question"; 
    private static final String KEY_ANSWER = "answer"; //correct option 
    private static final String KEY_OPTA= "opta"; //option a 
    private static final String KEY_OPTB= "optb"; //option b 
    private static final String KEY_OPTC= "optc"; //option c 
    private SQLiteDatabase dbase; 
    public DbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     dbase=db; 
     String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_QUEST + " (" 
       + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUES 
       + " TEXT, " + KEY_ANSWER+ " TEXT, "+KEY_OPTA +" TEXT, " 
       +KEY_OPTB +" TEXT, "+KEY_OPTC+" TEXT)"; 
     db.execSQL(sql); 
     addQuestions(); 
     db.close(); 
    } 
    private void addQuestions() 
    { 
     Question q1=new Question("Which company is the largest manufacturer" + 
       " of network equipment?","HP", "IBM", "CISCO", "CISCO"); 
     this.addQuestion(q1); 
     Question q2=new Question("Which of the following is NOT " + 
       "an operating system?", "SuSe", "BIOS", "DOS", "BIOS"); 
     this.addQuestion(q2); 
     Question q3=new Question("Which of the following is the fastest" + 
       " writable memory?","RAM", "FLASH","Register","Register"); 
     this.addQuestion(q3); 
     Question q4=new Question("Which of the following device" + 
       " regulates internet traffic?", "Router", "Bridge", "Hub","Router"); 
     this.addQuestion(q4); 
     Question q5=new Question("Which of the following is NOT an" + 
       " interpreted language?","Ruby","Python","BASIC","BASIC"); 
     this.addQuestion(q5); 
     Question q6 = new Question("2+2 is equal to" + 
       " which of the following?","0","22","4","4"); 
     this.addQuestion(q6); 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldV, int newV) { 
     // Drop older table if existed 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST); 
     // Create tables again 
     onCreate(db); 
    } 
    // Adding new question 
    public void addQuestion(Question quest) { 
     //SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_QUES, quest.getQUESTION()); 
     values.put(KEY_ANSWER, quest.getANSWER()); 
     values.put(KEY_OPTA, quest.getOPTA()); 
     values.put(KEY_OPTB, quest.getOPTB()); 
     values.put(KEY_OPTC, quest.getOPTC()); 
     // Inserting Row 
     dbase.insert(TABLE_QUEST, null, values); 
    } 
    public List<Question> getAllQuestions() { 
     List<Question> quesList = new ArrayList<Question>(); 
     // Select All Query 
     String selectQuery = "SELECT * FROM " + TABLE_QUEST; 
     dbase=this.getReadableDatabase(); 
     Cursor cursor = dbase.rawQuery(selectQuery, null); 
     // looping through all rows and adding to list 
     if (cursor.moveToFirst()) { 
      do { 
       Question quest = new Question(); 
       quest.setID(cursor.getInt(0)); 
       quest.setQUESTION(cursor.getString(1)); 
       quest.setANSWER(cursor.getString(2)); 
       quest.setOPTA(cursor.getString(3)); 
       quest.setOPTB(cursor.getString(4)); 
       quest.setOPTC(cursor.getString(5)); 
       quesList.add(quest); 
      } while (cursor.moveToNext()); 
     } 
     // return quest list 
     return quesList; 
    } 
    public int rowcount() 
    { 
     int row=0; 
     String selectQuery = "SELECT * FROM " + TABLE_QUEST; 
     SQLiteDatabase db = this.getWritableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     row=cursor.getCount(); 
     return row; 
    } 
} 

我更改代碼從這個:

private void addQuestions() 
    { 
     Question q1=new Question("Which company is the largest manufacturer" + 
       " of network equipment?","HP", "IBM", "CISCO", "CISCO"); 
     this.addQuestion(q1); 
     Question q2=new Question("Which of the following is NOT " + 
       "an operating system?", "SuSe", "BIOS", "DOS", "BIOS"); 
     this.addQuestion(q2); 
     Question q3=new Question("Which of the following is the fastest" + 
       " writable memory?","RAM", "FLASH","Register","Register"); 
     this.addQuestion(q3); 
     Question q4=new Question("Which of the following device" + 
       " regulates internet traffic?", "Router", "Bridge", "Hub","Router"); 
     this.addQuestion(q4); 
     Question q5=new Question("Which of the following is NOT an" + 
       " interpreted language?","Ruby","Python","BASIC","BASIC"); 
     this.addQuestion(q5); 
    } 

這一個,我嘗試添加問題6,但它並沒有顯示出來:

private void addQuestions() 
    { 
     Question q1=new Question("Which company is the largest manufacturer" + 
       " of network equipment?","HP", "IBM", "CISCO", "CISCO"); 
     this.addQuestion(q1); 
     Question q2=new Question("Which of the following is NOT " + 
       "an operating system?", "SuSe", "BIOS", "DOS", "BIOS"); 
     this.addQuestion(q2); 
     Question q3=new Question("Which of the following is the fastest" + 
       " writable memory?","RAM", "FLASH","Register","Register"); 
     this.addQuestion(q3); 
     Question q4=new Question("Which of the following device" + 
       " regulates internet traffic?", "Router", "Bridge", "Hub","Router"); 
     this.addQuestion(q4); 
     Question q5=new Question("Which of the following is NOT an" + 
       " interpreted language?","Ruby","Python","BASIC","BASIC"); 
     this.addQuestion(q5); 
     Question q6 = new Question("2+2 is equal to" + 
       " which of the following?","0","22","4","4"); 
     this.addQuestion(q6); 
    } 

有人能幫我嗎?我正在使用的教程是從這個website,我正在使用的代碼可以從這個link

+1

按'不顯示'你的意思是你沒有看到數據庫中的問題? – zgc7009

+1

你需要發佈更多的代碼並更好地解釋。比如'addQuestion()'實際上做了什麼以及你想如何顯示這些。 – codeMagic

+0

codeMagic不,我的意思是它沒有出現,當我運行應用程序,我發佈了更多的代碼 –

回答

1

我覺得你的問題是這段代碼

公共無效的onClick(視圖v){...如果(QID < 5){ currentQ = quesList.get(QID); setQuestionView(); } else {....

如果問題ID小於5,則顯示下一個問題,否則顯示您的分數。由於您現在有6個問題,因此您應該將if qid < 5更改爲if qid < 6

+1

這是一個簡單的靜態修復正確的,但對於更動態的東西,他可以做類似'if(qid zgc7009

+1

@ zgc7009同意,只是不想爲初學者添加太多內容。 :-) –

+0

感謝您的幫助,但現在,該應用程序正在拋出一個致命異常IndexOutOfBounds異常,無效索引5大小爲五。現在我該怎麼做? –