Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

Android: onClickListener does not work as programmed

Quiz on android: I am setting an onClickListener to monitor which answer button is pressed and i am trying to access the correct answer from a table to check if score is given or not. Problem is question doesnt change(number in this case).

//variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private TextView questionBox;
private Button startButton, answerButton1, answerButton2, answerButton3, answerButton4;
private RadioGroup radioGroup1;
private RadioButton difficulty_easy_radio, difficulty_medium_radio, difficulty_hard_radio;
private String difficulty = "easy";
private int score = 0;
int pointer = 0;
public String correct; // QuestionTables.questionTable[pointer].getCorrectAnswer();
//variables ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

startButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(v.getId() == startButton.getId())
            {
                //setting the elements of the layout
                setContentView(R.layout.activity_playing);
                final View controlsView = findViewById(R.id.fullscreen_content_controls2);
                final View contentView = findViewById(R.id.fullscreen_content2);

                //connecting layout elements with variables
                questionBox = (TextView) findViewById(R.id.question_box);
                answerButton1 = (Button) findViewById(R.id.answer_button1);
                answerButton2 = (Button) findViewById(R.id.answer_button2);
                answerButton3 = (Button) findViewById(R.id.answer_button3);
                answerButton4 = (Button) findViewById(R.id.answer_button4);

                //changing questions *****"pointer" and "correct" are declared globally to avoid:
                //Cannot refer to a non-final variable inside an inner class defined in a different method ERROR*****
                // don't forget to i++ as i<QuestionTables.questionTable.length=10

                //set first question
                QuestionTables.shuffleAnswers(pointer);
                questionBox.setText(""+pointer);
                answerButton1.setText(QuestionTables.questionAnswers[0]);
                answerButton2.setText(QuestionTables.questionAnswers[1]);
                answerButton3.setText(QuestionTables.questionAnswers[2]);
                answerButton4.setText(QuestionTables.questionAnswers[3]);
                correct = QuestionTables.questionTable[pointer].getCorrectAnswer();

                //button listener
                controlsView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        switch(v.getId())
                        {
                            case R.id.answer_button1:
                            {
                                //check if correct
                                correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
                                int clickedId = v.getId();
                                Button clickedButton = (Button) findViewById(clickedId);
                                if(clickedButton.getText().equals(correct))
                                    score+=100;
                                pointer++;

                                //change question
                                QuestionTables.shuffleAnswers(pointer);
                                questionBox.setText(""+pointer);
                                answerButton1.setText(QuestionTables.questionAnswers[0]);
                                answerButton2.setText(QuestionTables.questionAnswers[1]);
                                answerButton3.setText(QuestionTables.questionAnswers[2]);
                                answerButton4.setText(QuestionTables.questionAnswers[3]);

                                break;
                            }

                            case R.id.answer_button2:
                            {
                                //check if correct
                                correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
                                int clickedId = v.getId();
                                Button clickedButton = (Button) findViewById(clickedId);
                                if(clickedButton.getText().equals(correct))
                                    score+=100;
                                pointer++;

                                //change question
                                QuestionTables.shuffleAnswers(pointer);
                                questionBox.setText(""+pointer);
                                answerButton1.setText(QuestionTables.questionAnswers[0]);
                                answerButton2.setText(QuestionTables.questionAnswers[1]);
                                answerButton3.setText(QuestionTables.questionAnswers[2]);
                                answerButton4.setText(QuestionTables.questionAnswers[3]);

                                break;
                            }

                            case R.id.answer_button3:
                            {
                                //check if correct
                                correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
                                int clickedId = v.getId();
                                Button clickedButton = (Button) findViewById(clickedId);
                                if(clickedButton.getText().equals(correct))
                                    score+=100;
                                pointer++;

                                //change question
                                QuestionTables.shuffleAnswers(pointer);
                                questionBox.setText(""+pointer);
                                answerButton1.setText(QuestionTables.questionAnswers[0]);
                                answerButton2.setText(QuestionTables.questionAnswers[1]);
                                answerButton3.setText(QuestionTables.questionAnswers[2]);
                                answerButton4.setText(QuestionTables.questionAnswers[3]);

                                break;
                            }

                            case R.id.answer_button4:
                            {
                                //check if correct
                                correct = QuestionTables.questionTable[pointer].getCorrectAnswer();
                                int clickedId = v.getId();
                                Button clickedButton = (Button) findViewById(clickedId);
                                if(clickedButton.getText().equals(correct))
                                    score+=100;
                                pointer++;

                                //change question
                                QuestionTables.shuffleAnswers(pointer);
                                questionBox.setText(""+pointer);
                                answerButton1.setText(QuestionTables.questionAnswers[0]);
                                answerButton2.setText(QuestionTables.questionAnswers[1]);
                                answerButton3.setText(QuestionTables.questionAnswers[2]);
                                answerButton4.setText(QuestionTables.questionAnswers[3]);

                                break;
                            }
                        }
                    }
                });
            }       
        }
    });

Note that startButton is in the first layout of the activity and when is pressed this layout comes next.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have a lot of code, but I'm assuming that this is the problem:

controlsView.setOnClickListener(new View.OnClickListener() {

You give controlsView a private OnClickListener then expect it to handle click events from all the buttons. This will fail, since the other buttons aren't using this OnClickListener.

Make a shared listener.

View.OnClickListener listener = new View.OnClickListener() {//..

Then pass it off to the rest of the buttons.

answerButton1.setOnClickListener (listener);
answerButton2.setOnClickListener (listener);
//etc

You can also make the Activity implement the OnClickListener interface.

public class MyActivity extends Activity implements OnClickListener{

@Override
public void onClick (View v)
{
 //implementation
}

Then to set the OnClickListener, you pass off the Activity instance instead.

answerButton1.setOnClickListener (MyActivity.this);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...