【问题标题】:How to display dynamically Radio Buttons in different Radio Grpups using SQLite in Android如何在 Android 中使用 SQLite 在不同的单选组中动态显示单选按钮
【发布时间】:2015-10-27 12:13:07
【问题描述】:

我正在尝试构建一个包含如下问题和答案的测试应用:

问题1:
单选按钮 - 问题 1 的答案 1
单选按钮 - 问题 1 的答案 2
单选按钮 - 问题 1 的答案 3
单选按钮 - 问题 1 的答案 4
问题2:
单选按钮 - 问题 2 的答案 1
单选按钮 - Question2 的 Answer2
单选按钮 - 问题 2 的答案 3
单选按钮 - Question2 的 Answer4

等等。问题是,当我尝试遍历数据库结果时,结果显示如下:

问题1:
单选按钮 - 问题 1 的答案 1
单选按钮 - 问题 1 的答案 2
单选按钮 - 问题 1 的答案 3
单选按钮 - 问题 1 的答案 4
单选按钮 - 问题 2 的答案 1
单选按钮 - Question2 的 Answer2
单选按钮 - 问题 2 的答案 3
单选按钮 - 问题 2 的答案 4
问题2:
单选按钮 - 问题 1 的答案 1
单选按钮 - 问题 1 的答案 2
单选按钮 - 问题 1 的答案 3
单选按钮 - 问题 1 的答案 4
单选按钮 - 问题 2 的答案 1
单选按钮 - Question2 的 Answer2
单选按钮 - 问题 2 的答案 3
单选按钮 - Question2 的 Answer4

如何为每个问题仅显示 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;
}

谢谢!

【问题讨论】:

    标签: android sqlite loops android-radiogroup android-radiobutton


    【解决方案1】:

    你的错误在这里

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

    【讨论】:

    • 好吧,在创建一个 Answer 对象时,您还应该记住它回答的是哪个问题。将Question 字段添加到Answer 类。
    • 我完全按照你说的尝试了,但它给了我一个错误:“错误:找不到符号变量答案”我也尝试更改 SQLite 语句,但没有运气:|
    • 我的错误。而不是qn.Answer,您应该参考an.Question。当然,您应该使用 Question 字段扩展您的 Answer 类,并将其正确存储在数据库中。
    • 我是 android 编程新手,我很困惑。我希望解决这个问题大约 7 个小时:|在数据库中,在 TABLE_ANSWERS 中,如果我存储 question_id,我已经有一个字段。但是我需要在这段代码中添加什么来解决这个问题?
    • 终于解决了。我添加了这样的 if 语句:for (Answer an : answers) { if (qn.getID() == an.getQuestion_id_answer()) { String answers_log = " " + an.getAnswer(); radioButton[i] = new RadioButton(this); radioGroup.addView(radioButton[i]); radioButton[i].setText(answers_log); } }
    猜你喜欢
    • 1970-01-01
    • 2018-04-17
    • 2015-12-16
    • 1970-01-01
    • 2016-02-10
    • 1970-01-01
    • 2018-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多