Reputation: 716
I am trying to create a class to reuse for reviews/quizzes that have multiple choice options in the form of radio buttons.
Using the following function I have no problems on a single activity
public void onClickListenerButton(){
radioGroupReview = (RadioGroup) findViewById(R.id.radioGroupReview);
btnReviewSubmit = (Button) findViewById(R.id.btnReviewSubmit);
btnReviewSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selection = radioGroupReview.getCheckedRadioButtonId();
radio_button = (RadioButton) findViewById(selection);
if(selection==R.id.radioCorrectAnswer) {
textrReviewResult.setText("Correct!");
btnReviewContinue.setVisibility(View.VISIBLE);
btnReviewSubmit.setVisibility(View.GONE);
} else {
textReviewResult.setText("Try Again.");
}
}
});
}
Applying this to a class has proven difficult as I am unsure how to replace the findViewById(selection) therefore throwing off the If/Else logic below.
public class ReviewLogic {
public void onClickListenerButton(final Button btnSubmit, final Button btnContinue, final RadioGroup radioGroup,final RadioButton radioButton, final TextView result){
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int selection = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selection);
if(selection==R.id.radioCorrectAnswer) {
result.setText("Correct!");
btnContinue.setVisibility(View.VISIBLE);
btnSubmit.setVisibility(View.GONE);
} else {
result.setText("Try Again.");
}
}
});
}
}
What substitutions can I consider to make the logic still work but exclude findViewById?
Thank you
Upvotes: 0
Views: 89
Reputation: 2545
You have to pass activity object to onClickListenerButton funtion
the access radioButton by
radioButton = (RadioButton)activity.findViewById(selection);
Upvotes: 0
Reputation: 2608
You can try to add radioGroup
before findViewById
so you will have something like:
radioButton = (RadioButton) radioGroup.findViewById(selection);
Upvotes: 1