How to get the id of selected radio button in android?

前端 未结 7 1882
遇见更好的自我
遇见更好的自我 2020-12-17 03:23

I am working on quiz application in android. We have created Select.java page which displays the questions and options(with radio buttons) from sqlite datab

7条回答
  •  有刺的猬
    2020-12-17 03:57

    Hmmm, just add one more member variable in UserBO to store selected answer.

    Class UserBO {
    
    private int userID;
    private String userName;
    private String question;
    private String option1;
    private String option2;
    private String option3;
    
    private int answerID;
    
    //create getter and setters for above member variables
    } 
    

    then within onclick listener of Adapter class, do like as following

    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
    
            @Override
            public void onCheckedChanged(RadioGroup radioGroup,
                    int radioButtonID) {
            switch(radioButtonID) {
                    case R.id.option1:
                           listItem.setAnswerID(1);
                break;
            case R.id.option2:
                    listItem.setAnswerID(2);
                break;
    
                     }
            }
        });
    

    then change your header constructor to receive userarraylist (which contains user details with answer)

    ArrayList userList;
    Header(Context context, AttributeSet attrs, ArrayList userALt) {
    userList = userAL;
    }
    
    //on next button click
    
    onclick() {
        for(UserBO userObj: userList) {
            if (userObj.getAnswerID != 0)
             Log.d("AnswerID", userObj.getAnswerID);
        }
    }
    

    it just like sudo code.. i hope this will help u..

提交回复
热议问题