我正在尝试使用这样的问题和答案构建测试应用程序:
问题1:
单选按钮 - 问题1的答案1
单选按钮 - 问题1的答案2
单选按钮 - 问题1的答案3
单选按钮 - 问题1的答案4
问题2:
单选按钮 - 问题2的答案1
单选按钮 - 问题2的答案2
单选按钮 - 问题2的答案3
单选按钮 - Question2的答案4
等等。问题是,当我试图循环数据库结果时,结果显示如下:
问题1:
单选按钮 - 问题1的答案1
单选按钮 - 问题1的答案2
单选按钮 - 问题1的答案3
单选按钮 - 问题1的答案4
单选按钮 - 问题2的答案1
单选按钮 - 问题2的答案2
单选按钮 - 问题2的答案3
单选按钮 - 问题2的答案4
问题2:
单选按钮 - 问题1的答案1
单选按钮 - 问题1的答案2
单选按钮 - 问题1的答案3
单选按钮 - 问题1的答案4
单选按钮 - 问题2的答案1
单选按钮 - 问题2的答案2
单选按钮 - 问题2的答案3
单选按钮 - Question2的答案4
如何为每个问题仅显示4个单选按钮(4个答案)?这是我的MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHandler db = new DBHandler(this);
// Inserting Questions
db.addQuestion(new Question("Question1?", 1));
db.addQuestion(new Question("Question2?", 2));
db.addQuestion(new Question("Question3?", 3));
db.addQuestion(new Question("Question4?", 4));
// Inserting Answers / Correct Answer (1 for true, 0 for false)
db.addAnswer(new Answer("Answer1 for Question1", 1, 0));
db.addAnswer(new Answer("Answer2 for Question1", 1, 0));
db.addAnswer(new Answer("Answer3 for Question1", 1, 1));
db.addAnswer(new Answer("Answer4 for Question1", 1, 0));
db.addAnswer(new Answer("Answer1 for Question2", 2, 0));
db.addAnswer(new Answer("Answer2 for Question2", 2, 0));
db.addAnswer(new Answer("Answer3 for Question2", 2, 1));
db.addAnswer(new Answer("Answer4 for Question2", 2, 0));
// Reading all Questions
List<Question> questions = db.getAllQuestions();
// Reading all Answers
List<Answer> answers = db.getAllAnswers();
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
for (Question qn : questions) {
String question_log = " Question: " + qn.getQuestion();
//Create Question Text
TextView question = new TextView(this);
question.setText(question_log);
question.setTextColor(Color.BLUE);
linearLayout.addView(question);
//Create Radio Button Answers
final RadioButton[] radioButton = new RadioButton[4];
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (int i = 0; i < 1; i++) {
for (Answer an : answers) {
String answers_log = " " + an.getAnswer();
radioButton[i] = new RadioButton(this);
radioGroup.addView(radioButton[i]);
radioButton[i].setText(answers_log);
}
}
linearLayout.addView(radioGroup);
}
}
这是我的DBHandler:
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "iqTest";
private static final String TABLE_QUESTIONS = "questions";
// Question Table Columns Names
private static final String KEY_QUESTION_ID = "question_id";
private static final String KEY_QUESTION = "question";
private static final String KEY_ORDER_NO = "order_no";
private static final String TABLE_ANSWERS = "answers";
// Answer Table Columns Names
private static final String KEY_ANSWER_ID = "answer_id";
private static final String KEY_ANSWER = "answer";
private static final String KEY_QUESTION_ID_ANSWER = "question_id_answer";
private static final String KEY_CORRECT_ANSWER = "correct_answer";
<pre>public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_QUESTIONS_TABLE = "CREATE TABLE " + TABLE_QUESTIONS + "("
+ KEY_QUESTION_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_QUESTION + " TEXT, "
+ KEY_ORDER_NO + " INTEGER " + ")";
String CREATE_ANSWERS_TABLE = "CREATE TABLE " + TABLE_ANSWERS + "("
+ KEY_ANSWER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + KEY_ANSWER + " TEXT, "
+ KEY_QUESTION_ID_ANSWER + " INTEGER, " + KEY_CORRECT_ANSWER + " INTEGER " + ")";
db.execSQL(CREATE_QUESTIONS_TABLE);
db.execSQL(CREATE_ANSWERS_TABLE);
}
// Upgrading Database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTIONS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_ANSWERS);
onCreate(db);
}
// Adding New Question
void addQuestion(Question question) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_QUESTION, question.getQuestion());
values.put(KEY_ORDER_NO, question.getorderNo());
db.insert(TABLE_QUESTIONS, null, values);
db.close();
}
// Getting All Questions
public List<Question> getAllQuestions() {
List<Question> questionList = new ArrayList<Question>();
String selectQuery = "SELECT * FROM " + TABLE_QUESTIONS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Question question = new Question();
question.setID(Integer.parseInt(cursor.getString(0)));
question.setQuestion(cursor.getString(1));
question.setorderNo(Integer.parseInt(cursor.getString(2)));
// Adding Questions to List
questionList.add(question);
} while (cursor.moveToNext());
}
return questionList;
}
// Adding New Answer
void addAnswer(Answer answer) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ANSWER, answer.getAnswer());
values.put(KEY_QUESTION_ID_ANSWER, answer.getQuestion_id_answer());
values.put(KEY_CORRECT_ANSWER, answer.getCorrect_answer());
db.insert(TABLE_ANSWERS, null, values);
db.close();
}
// Getting All Answers
public List<Answer> getAllAnswers() {
List<Answer> answerList = new ArrayList<Answer>();
String selectQuery = "SELECT * FROM " + TABLE_ANSWERS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Answer answer = new Answer();
answer.setAnswer_id(Integer.parseInt(cursor.getString(0)));
answer.setAnswer(cursor.getString(1));
answer.setQuestion_id_answer(Integer.parseInt(cursor.getString(2)));
answer.setCorrect_answer(Integer.parseInt(cursor.getString(3)));
// Adding Questions to List
answerList.add(answer);
} while (cursor.moveToNext());
}
return answerList;
}
谢谢!
答案 0 :(得分:2)
你的错误就在这里
for (Answer an : answers)
您应首先过滤answers
以仅包含当前qn
我会像这样编辑你的代码的一部分:
//Create Radio Button Answers
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.VERTICAL);
for (Answer an : answers) {
if (an.Question == qn) {
String answers_log = " " + an.getAnswer();
RadioButton radioButton = new RadioButton(this);
radioButton.setText(answers_log);
radioGroup.addView(radioButton);
}
}