gpt4 book ai didi

java - Android Quiz App 的答案即使已正确保存也无法正常工作

转载 作者:行者123 更新时间:2023-11-29 04:25:42 28 4
gpt4 key购买 nike

我无法弄清楚我的应用程序出了什么问题。我有一个测验应用程序(现在只是随机输入问题和答案),所有问题和答案都存储在 SQLite 数据库中。当我测试该应用程序时,它说答案是错误的,即使它们是正确的。

答案存储为 A、B、C、D,我不确定如何使其与真实答案相符。

这是我的检查答案代码:

  private void checkAnswer() {
//TODO: check answer
RadioGroup grp = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton answer = (RadioButton) findViewById(grp.getCheckedRadioButtonId());
if (currentQuestion.getAnswer().equals(answer.getText())) {
Toast.makeText(getApplicationContext(), R.string.correct_toast, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Chosen answer " + answer.getText().toString() +
" Real answer " + currentQuestion.getAnswer(),
Toast.LENGTH_LONG).show();
}
currentQuestion = quesList.get(mQuestionId);
setQuestionView();

}

这里是在onCreate中调用的:

    nextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkAnswer();
}
});

这是数据库助手类:

public class DbHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "realEstateQuiz";
// 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 static final String KEY_OPTD = "optd"; //option d

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," + KEY_OPTD + " TEXT)";
db.execSQL(sql);
addQuestions(db);
//db.close();
}

private void addQuestions(SQLiteDatabase db) {
Question q1 = new Question("How many houses can I buy?",
"1", "2", "3", "4",
"A");
this.addQuestion(q1, db);
Question q2 = new Question("Which of the following is NOT an option ",
"Apartments", "Houses", "Duplexes", "Rentals",
"D");
this.addQuestion(q2, db);
Question q3 = new Question("Which of the following is the fastest way of buying",
"Credit card", "Loan", "Cash", "Handshakes",
"C");
this.addQuestion(q3, db);
Question q4 = new Question("Should you use an agent or the internet",
"Agent", "Internet", "Neither", "Both",
"A");
this.addQuestion(q4, db);
Question q5 = new Question("Which of the following is not an available language",
"English", "Italian", "Spanish", "Hungarian",
"C");
this.addQuestion(q5, db);
Question q6 = new Question("Question 6",
"Answer 1", "Answer 2 - correct", "Answer 3", "Answer 4",
"B");
this.addQuestion(q6, db);
}

@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) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUES, quest.getQuestion());
values.put(KEY_ANSWER, quest.getAnswer());
values.put(KEY_OPTA, quest.getOptionA());
values.put(KEY_OPTB, quest.getOptionB());
values.put(KEY_OPTC, quest.getOptionC());
values.put(KEY_OPTD, quest.getOptionD());

// Inserting Row
db.insert(TABLE_QUEST, null, values);
}

感谢您的帮助!

最佳答案

您需要在 EditText/TextView 上应用 toString 来获取文本,否则您只是将您的答案与 CharSequence 进行比较(使用 getText ) 可能总是给你错误的引用

if (currentQuestion.getAnswer().equals(answer.getText().toString())) 
// ^^^^^^^^^^^

注意:RadioButton 是 TextView 的后代,因此 getText 来自返回 CharSequence

的 TextView

更新:为简单起见,将您的问题与答案而不是选项一起存储

Question q2 = new Question("Which of the following is NOT an option ",
"Apartments", "Houses", "Duplexes", "Rentals",
"Apartments");

并从 addQuestions 中删除 db = this.getWritableDatabase(); ,这是多余的,不是必需的。

关于java - Android Quiz App 的答案即使已正确保存也无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46449359/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com