当我在Android ListView,
RadioButton
中向下滚动时无法选择。我在set change check listener上应用set selected true,不工作,需要帮助。谢谢你。
我使用了一个使用4 RadioButton
的自定义适配器并设置了mcq answer选项。 50 mcq,但在ListView
中滚动时,而不是之前选择的项目。自动刷新。任何人都可以建议我能做些什么。
public class ModeltestDetailsAdapter extends ArrayAdapter {
private final Context context;
private final List<AllData> values;
public static ArrayList<String> selectedAnswers;
public ModeltestDetailsAdapter(Context context, List<AllData> values) {
super(context, R.layout.list_details_modeltest, values);
this.context = context;
this.values = values;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < values.size(); i++) {
selectedAnswers.add("Not Attempted");
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// urveyDataHelper mDbHelper = new SurveyDataHelper(context);
final ViewGroup p;
p = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.list_details_modeltest, parent, false);
TextView questions = (TextView) rowView.findViewById(R.id.questions);
TextView answer = (TextView) rowView.findViewById(R.id.ans);
final RadioGroup radioGroup = (RadioGroup) rowView.findViewById(R.id.radiogroup);
RadioButton radioButton;
String result;
final RadioButton optiona= (RadioButton) rowView.findViewById(R.id.optiona);
final RadioButton optionb= (RadioButton) rowView.findViewById(R.id.optionb);
RadioButton optionc= (RadioButton) rowView.findViewById(R.id.optionc);
RadioButton optiond= (RadioButton) rowView.findViewById(R.id.optiond);
questions.setText(values.get(position).getQues());
answer.setText(values.get(position).getAns());
optiona.setText(values.get(position).getOpt_one());
optionb.setText(values.get(position).getOpt_two());
optionc.setText(values.get(position).getOpt_three());
optiond.setText(values.get(position).getOpt_four());
optionc.setChecked(true);
optiona.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked) {
optiona.setChecked(true);
//notifyDataSetChanged();
Toast.makeText(
context,
"Answer is ." + buttonView.getText().toString(),
Toast.LENGTH_LONG).show();
if (values.get(position).getAns().equals(buttonView.getText().toString())) {
selectedAnswers.set(position, "Correct");
} else {
selectedAnswers.set(position, "Wrong");
}
}else {
optiona.setChecked(false);
}
}
});
optionb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// set Yes values in ArrayList if RadioButton is checked
if (isChecked) {
optionb.setChecked(true);
// notifyDataSetChanged();
Toast.makeText(
context,
"Answer is ." + buttonView.getText().toString(),
Toast.LENGTH_LONG).show();
if (values.get(position).getAns().equals(buttonView.getText().toString())) {
selectedAnswers.set(position, "Correct");
} else {
selectedAnswers.set(position, "Wrong");
}
}
else {
optionb.setChecked(false);
}
}
});
questions.setTag(position);
answer.setTag(position);
optiona.setTag(position);
optionb.setTag(position);
optionc.setTag(position);
optiond.setTag(position);
//radioButton.setTag(position);
optiona.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int pos = (Integer) v.getTag();
optiona.setChecked(true);
optiona.setSelected(true);
((ListView) p).performItemClick(v, pos, 0);
}
});
answer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int pos = (Integer) v.getTag();
((ListView) p).performItemClick(v, pos, 0);
}
});
questions.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int pos = (Integer) v.getTag();
((ListView) p).performItemClick(v, pos, 0);
}
});
return rowView;
}
}
答案 0 :(得分:0)
在AllData
模型中,选择另一个变量,
int selectedAnswer = -1; // -1 as default for not selected answer.
现在,在Adapter类中,在getView()
方法中,执行以下实现。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewGroup p;
p = parent;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.list_details_modeltest, parent, false);
TextView questions = (TextView) rowView.findViewById(R.id.questions);
TextView answer = (TextView) rowView.findViewById(R.id.ans);
final RadioGroup radioGroup = (RadioGroup) rowView.findViewById(R.id.radiogroup);
RadioButton radioButton;
String result;
final RadioButton optiona= (RadioButton) rowView.findViewById(R.id.optiona);
final RadioButton optionb= (RadioButton) rowView.findViewById(R.id.optionb);
RadioButton optionc= (RadioButton) rowView.findViewById(R.id.optionc);
RadioButton optiond= (RadioButton) rowView.findViewById(R.id.optiond);
questions.setText(values.get(position).getQues());
answer.setText(values.get(position).getAns());
optiona.setText(values.get(position).getOpt_one());
optionb.setText(values.get(position).getOpt_two());
optionc.setText(values.get(position).getOpt_three());
optiond.setText(values.get(position).getOpt_four());
if(values.get(position).getSelectedAnswer() == 1){
optiona.setSelected(true);
}
else if(values.get(position).getSelectedAnswer() == 2){
optionb.setSelected(true);
}
else if(values.get(position).getSelectedAnswer() == 3){
optionc.setSelected(true);
}
else if(values.get(position).getSelectedAnswer() == 4){
optiond.setSelected(true);
}
optiona.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
values.get(position).setSelectedAnswer(1);
....
}else {
optiona.setChecked(false);
}
}
});
....
return rowView;
}