-1

i'm making an android quiz app. there are have one question an four answers, one of four is true. when user choose wrong answer the game is end, so i want to insert this question and the true answer to database to show it in the listview. but it so difficult with me, please help me some codes. this is my choose answer method:

private void onOptionSelected(String option){

    if(!isGamePaused && !isGameEnded) { 
        ATriviaQuestion tTQuestion = myListOfTriviaQuestions.get(currentQuestionNumber);
        if(option.equals(tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1))) {
            correct += 1;
            remainingTime = mySecondsPassed;
            totalPoints += remainingTime * pointsPerRemainingSecond; 
            totalPoints += pointsPerCorrectAnswer;  
            Toast.makeText(PlayGame.this, "chuan cmnr", Toast.LENGTH_SHORT).show();
        }
        else{questionsAsked = totalQuestions;
            incorrect += 1;
            totalPoints -= pointsPerWrongAnswer;
            Toast.makeText(PlayGame.this,tTQuestion.GetOptions().get(tTQuestion.GetAnswer() - 1), Toast.LENGTH_LONG).show();
            isGameEnded=true;
            EndGame();

        }

        mySecondsPassed = config.GetTimeToAnswer();
        UpdateTimerColors(mySecondsPassed);


        if(questionsAsked >= totalQuestions){   
            isGameEnded=true;
            EndGame();
            DisplayResults();
        } else {                    

            currentQuestionNumber=GenerateQuestionNumber();
            PopulateQuestion();
        }

    }else{
        PausedOrEndedGameResponse();
    }
}

and this is my database.java

    public Quanly_congviec(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);

}

@Override
public void onCreate(SQLiteDatabase db) {

    String SQL_String = "CREATE TABLE " + ten_table + "("
            + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," 
            + KEY_NOIDUNG_CONGVIEC + " TEXT,"
            + KEY_THOIGIAN_THUCHIEN + " TEXT, "
            + KEY_MUCDO_QUANTRONG + " TEXT" + ")";
    db.execSQL(SQL_String);

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {

    db.execSQL("DROP TABLE IF EXISTS " + ten_table);


    onCreate(db);   
}


public List<Congviec> getAllCongviec() {
    List<Congviec> CongviecList = new ArrayList<Congviec>();
    String[] ds_dieukien_loc=null; 

    String selectQuery = "SELECT  * FROM " + ten_table;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, ds_dieukien_loc);


    if (cursor.moveToFirst()) {
        do {
            Congviec Congviec = new Congviec();
            Congviec.set_id(Integer.parseInt(cursor.getString(0)));
            Congviec.setNoidung_congviec(cursor.getString(1));
            Congviec.setThoigian_thuchien(cursor.getString(2));
            Congviec.setMucdo_quantrong(cursor.getString(3));

            CongviecList.add(Congviec);
        } while (cursor.moveToNext());
    }
    db.close();
    return CongviecList;    

}

public void them_congviec(Congviec congviec) {
     SQLiteDatabase db = this.getWritableDatabase();
     String nullColumnHack=null; 
     ContentValues values = new ContentValues();
     if (congviec.get_id()!=-1) values.put(KEY_ID,congviec.get_id());
     values.put(KEY_NOIDUNG_CONGVIEC, congviec.getNoidung_congviec()); 
     values.put(KEY_THOIGIAN_THUCHIEN, congviec.getThoigian_thuchien()); 
     values.put(KEY_MUCDO_QUANTRONG, congviec.getMucdo_quantrong()); 
     db.insert(ten_table, nullColumnHack, values);
     db.close();        
}


public void xoatatca_congviec() {
     SQLiteDatabase db = this.getWritableDatabase();
     String whereClause="";
     String[] whereArgs=null;
     db.delete(ten_table, whereClause, whereArgs);          
     db.close();        
}

public void xoa_1_congviec(int id){
    SQLiteDatabase db= this.getWritableDatabase();
    db.execSQL("delete from  " + ten_table + " where _id="+id);
    db.close();
}

public int sua_1_congviec(Congviec congviec) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(KEY_NOIDUNG_CONGVIEC, congviec.getNoidung_congviec());  
     values.put(KEY_THOIGIAN_THUCHIEN, congviec.getThoigian_thuchien());  
     values.put(KEY_MUCDO_QUANTRONG, congviec.getMucdo_quantrong());  

     String whereClause=KEY_ID + "=?"; 
     String[] whereArgs={String.valueOf(congviec.get_id())}; 
     return db.update(ten_table, values, whereClause, whereArgs); 
    } 

}

in the database table has three row but i want to insert the question in row 1, the true answer in row 2 and row three is blank.

4

1 に答える 1

0

これに最適なデータベース設計は、各質問とそのすべての回答に対して 1 つの行を作成することです。4 つの回答があり、そのうちの 1 つが正しい質問の場合、列名は次のようになります。

_ID 質問 正解 正解 不正解1 不正解2 不正解3 順番

(質問が表示される順序を設定するには、 order を使用します)。

別の方法として、1 つのテーブルに質問を格納し、別のテーブルに各質問の回答を格納することもできますが、最初のオプションは処理がはるかに簡単です。

最後の注意: 母国語が英語でない場合は、変数名と列名に母国語を使用するのが自然です。残念ながら、ほとんどの開発者は英語を使用しています。あなたのコードは私にとってははるかに読みにくいです.なぜなら、私はベトナム語が何であるかを読むことができないからです. それは不公平ですが、そこにあります。

于 2012-11-09T16:57:34.450 に答える