2016-11-07 9 views
1

私はAndroidスタジオとJavaの初心者ですが、私の最初のAndroidアプリケーションはほぼ完成ですが、5つ以上の質問を追加しようとしていますが、質問6それは表示されません。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; 
    } 
} 

は、私はこの1つのコードを変更します:私は質問6を追加しようとするが、それは表示されませんこの1、へ

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

ここに私のDbHelper.javaクラスからのコードは次のとおりです。

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

私はあなたの問題は、コード

ます。public void onClickの(ビューV){...もし(QID < 5){ currentQ = quesList.get(QID)のこのビットであると思います。setQuestionView(); } else {....

質問IDが5未満の場合は、次の質問を表示し、それ以外の場合はスコアを表示してください。今度は6つの質問があるのでif qid < 5if qid < 6に変更してください。

+1

これは単純な静的な修正には正しいですが、何かもっと動的に 'if(qid zgc7009

+1

@ zgc7009合意して、初心者のためにあまりにも多くを追加したくありませんでした。 :-) –

+0

助けていただきありがとうございますが、現在、アプリは私に致命的な例外を投げています。IndexOutOfBounds Exception、無効なインデックス5サイズは5です。今私は何をしますか? –

関連する問題