-1
我正在做一個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行中的真實答案是空白的。
對此不便表示抱歉,我的語言是越南語,非常感謝你回答:)。 – TranGia