I have 3 questions
private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] answer = new boolean[NUMBER_OF_QUESTIONS];
static boolean[] checked = new boolea
Where are you updating isAnswered array. In the code snippet you have provided, it updates only answer and checked arrays.
Probably that is why you are always getting false for all 3 array elements of isAnswered array.
Where are you updating isAnswered array. In the code snippet you have provided, it updates only answer and checked arrays.
Probably that is why you are always getting false for all 3 array elements of isAnswered array.
Hi Roman,
I believe you have multiple radio groups each with 3 radio buttons. If that is the case, just slight modification in you above code snippet should work for you.
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton1) {
Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
checked[0] = true;
answer[0] = true;
} else {
checked[0] = true;
answer[0] = false;
}
// update isAnswered[0] to true. as this gets invoked when you choose any radio button.
isAnswered[0] = true;
}
});
Please let me know if this helps.
I have implemented a quick class to validate your logic and found no problems in your implementation.
public class MainActivity extends AppCompatActivity {
private static int NUMBER_OF_QUESTIONS = 3;
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setBooleanValues();
allAnswersChecked();
}
private void setBooleanValues() {
isAnswered[0]=true;
isAnswered[1]=false;
isAnswered[2]=true;
}
private void allAnswersChecked() {
for (boolean radioAnswer : isAnswered) {
if (!radioAnswer) {
//return false;
Log.d("allAnswersChecked","false");
}
else {
Log.d("allAnswersChecked","true");
}
}
//return true;
}
}
See the log output :
05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: true
05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: false
05-12 16:43:10.299 8448-8448/com.example.myapplicationtest D/allAnswersChecked: true
So I recomment to print values and check if it is being correct set into your isAnswered array before calling allAnswersChecked().
Try this:
static boolean[] isAnswered = new boolean[NUMBER_OF_QUESTIONS]; // initially all false
.............
....................
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.radioButton1) {
Toast.makeText(getActivity(), "True", Toast.LENGTH_SHORT).show();
checked[0] = true;
answer[0] = true;
} else {
checked[0] = true;
answer[0] = false;
}
// Update value as user selected radio button
isAnswered[0] = true;
}
});
Do same for your other RadioGroups
(rg2, rg3, ......
). Hope this will help~