I am using custom ListView
with 2 TextView
and four RadioButtons
in a radio group. When I selected a RadioButton
I want to show that radio group to get invisible and answer TextView
gets visible. Its working perfectly but problem is when I scroll down and again scroll up the radio group gets visible by itself.
class CustomAdapter extends ArrayAdapter<String> {
public CustomAdapter(Context context, int resource, int textViewResourceId, ArrayList<String> objects) {
super(context, resource, textViewResourceId, objects);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View v = inflater.inflate(R.layout.row, parent, false);
TextView mcqsText = (TextView) v.findViewById(R.id.mcqsText);
final TextView ans2 = (TextView) v.findViewById(R.id.answer2);
final TextView anss = (TextView) v.findViewById(R.id.answer);
final RadioGroup rg = (RadioGroup) v.findViewById(R.id.radioGroup);
RadioButton opt1 = (RadioButton) v.findViewById(R.id.optA);
RadioButton opt2 = (RadioButton) v.findViewById(R.id.optB);
RadioButton opt3 = (RadioButton) v.findViewById(R.id.optC);
RadioButton opt4 = (RadioButton) v.findViewById(R.id.optD);
final ImageView ivcorrect= (ImageView) v.findViewById(R.id.ivcorrect);
final ImageView ivwrong= (ImageView) v.findViewById(R.id.ivwrong);
mcqsText.setText(mcqs.get(position));
opt1.setText(optA.get(position));
opt2.setText(optB.get(position));
opt3.setText(optC.get(position));
opt4.setText(optD.get(position));
anss.setText(ans.get(position));
ans2.setText("Correct answer is = "+ans.get(position));
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
int radioButtonID = group.getCheckedRadioButtonId();
View radioButton = group.findViewById(radioButtonID);
//find radiobutton position
int position = group.indexOfChild(radioButton);
RadioButton btn = (RadioButton) rg.getChildAt(position);
String selection = (String) btn.getText();
if (selection.equals(anss.getText().toString())) {
rg.setVisibility(View.GONE);
ans2.setVisibility(View.VISIBLE);
ivcorrect.setVisibility(View.VISIBLE);
correct++;
} else {
ivwrong.setVisibility(View.VISIBLE);
ans2.setVisibility(View.VISIBLE);
wrong++;
}
}
});
return v;
}
}