我正在制作一个Android测验应用。有一个问题有四个答案,其中四个是真的。当用户选择错误的答案时游戏结束,所以我想插入这个问题和数据库的真实答案,以便在列表视图中显示它。但是对我这么难,请帮我一些代码。这是我选择的答案方法:
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行的真实答案是空白的。
答案 0 :(得分:0)
最好的数据库设计是每个问题都有一行,所有的答案。对于有四个答案的问题,其中一个是正确的,列名可以是
_ID 题 正确答案 Incorrect1 Incorrect2 Incorrect3 为了
(我会使用命令设置问题出现的顺序)。
另一种方法是存储问题的一个表,另一个存储每个问题的答案,但第一个选项更易于处理。
最后说明: 如果您的第一语言不是英语,那么将您的母语用于变量和列名称是很自然的。唉,大多数开发人员都使用英语。你的代码对 me 的可读性要低得多,因为我无法阅读(我认为)越南语的内容。这是不公平的,但确实如此。