문제

I am a newbee in Android app creating world.

I am creating an app where multiple choice questions are there. And, it could be 25, 50 or 100 depend on what user chooses. Now in an Activity, there would a question with 4 options along with next/prev button to navigate to the next or previous question. How can I achieve the next/previous button function which will display the next/previous question in the same activity just updating the content with new question or previously answered question? Any help would be appreciated.

Thanks :)

도움이 되었습니까?

해결책

A simple way (although maybe not the best in other's opinions) is to define one Activity with a TextView for the question and a RadioGroup with RadioButtons for the answers. (And ofcourse the buttons.)

Then, whenever one of the buttons is pressed, dynamically change the contents of the TextView so that the right question appears and change the contents of the radiobuttons so that the possible answers appear. Store whichever RadioButton is chosen whenever the user presses one of the buttons.

You could store the questions and answers in a database. The onClickListener() assigned to the button should then get the next record in the database and use yourTextView.setText(theQuestionFromTheDB). Same holds for the answers. To store the chosen answer you can use

int selectedId = radioSexGroup.getCheckedRadioButtonId();

and then write that value to the database.

Instead of a database, you could also use collectiosn like an arrays in your code, but I think this will be kind of painful to read all the questions to it initially. This is ofcourse, depending on where you get the questions from. If it is a raw .txt file or something that you dynamically want to read into your app, then an array or ArrayList is not all that bad.

For an example of how to add and use RadioButtons in your activity UI, click here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top