mardi 27 octobre 2015

How to display dynamically Radio Buttons in different Radio Grpups using SQLite in Android

I'm trying to build a test app with questions and answers like this:

Question1:
Radio Button - Answer1 for Question1
Radio Button - Answer2 for Question1
Radio Button - Answer3 for Question1
Radio Button - Answer4 for Question1
Question2:
Radio Button - Answer1 for Question2
Radio Button - Answer2 for Question2
Radio Button - Answer3 for Question2
Radio Button - Answer4 for Question2

and so on. The problem is, when i am trying to loop trough the database results, the result is displayed like this:

Question1:
Radio Button - Answer1 for Question1
Radio Button - Answer2 for Question1
Radio Button - Answer3 for Question1
Radio Button - Answer4 for Question1
Radio Button - Answer1 for Question2
Radio Button - Answer2 for Question2
Radio Button - Answer3 for Question2
Radio Button - Answer4 for Question2
Question2:
Radio Button - Answer1 for Question1
Radio Button - Answer2 for Question1
Radio Button - Answer3 for Question1
Radio Button - Answer4 for Question1
Radio Button - Answer1 for Question2
Radio Button - Answer2 for Question2
Radio Button - Answer3 for Question2
Radio Button - Answer4 for Question2

How to display only 4 Radio Buttons (4 Answers) for each question? This is my 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);
    }
}

And this my 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;
}

Thanks!

Aucun commentaire:

Enregistrer un commentaire