-1

我正在制作一个安卓测验应用程序。有一个问题四个答案,四个答案之一是正确的。当用户选择错误答案时游戏结束,所以我想将这个问题和真实答案插入数据库以在列表视图中显示。但这对我来说太难了,请帮我一些代码。这是我选择的答案方法:

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

这是我的 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); 
    } 

}

在数据库表中有三行,但我想在第 1 行插入问题,第 2 行和第 3 行的真实答案为空白。

4

1 回答 1

0

最好的数据库设计是为每个问题及其所有答案保留一行。对于一个有四个答案的问题,其中一个是正确的,列名可以是

_ID 问题 CorrectAnswer Incorrect1 Incorrect2 Incorrect3 Order

(I'd use order to set the order in which questions should appear).

An alternative would be one table to store questions, and another to store answers for each question, but the first option is a lot more simple to handle.

Final note: If your first language isn't English, it's natural to use your native language for variable and column names. Alas, most developers use English. Your code is much less readable for me, because I can't read what is (I presume) Vietnamese. It's unfair, but there it is.

于 2012-11-09T16:57:34.450 回答