Radiogroup Doesnt Keep Properly State In Listview
I know about ListView recycling issue and try to keep my state in model, but it seems not to work. I've read many topics with the same problem, but their solutions dont work for me
Solution 1:
Before you are setting checked or unchecked to your RadioGroup
, you should disable the listener, like this. Hope this helps.
Change your adapter to this
publicclassRadioAdapterextendsArrayAdapter<Question> {
List<Question> mSource;
RadioGroup.OnCheckedChangeListener listener;
staticclassViewHolder {
TextView category = null;
TextView question = null;
RadioGroup rbGroup = null;
ViewHolder(View row) {
this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
this.question = (TextView) row.findViewById(R.id.tvQuestion);
this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
}
}
private LayoutInflater mInflater;
publicRadioAdapter(Context context, List<Question> mSource) {
super(context, R.layout.item_question, mSource);
mInflater = LayoutInflater.from(context);
this.mSource = mSource;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View row = convertView;
if (row == null) {
row = mInflater.inflate(R.layout.item_question, null);
holder = new ViewHolder(row);
listener = new RadioGroup.OnCheckedChangeListener() {
publicvoidonCheckedChanged(RadioGroup group, int checkedId) {
//Get position of current group
Integer pos = (Integer) group.getTag();
switch (checkedId) {
case R.id.rbIterationYes:
//Check 'YES' and uncheck 'NO'
mSource.get(pos).setYesChecked(true);
mSource.get(pos).setNoChecked(false);
break;
case R.id.rbIterationNo:
//Vice versa
mSource.get(pos).setNoChecked(true);
mSource.get(pos).setYesChecked(false);
break;
}
}
};
holder.rbGroup.setOnCheckedChangeListener(listener);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.rbGroup.setTag(new Integer(position));
holder.category.setText(mSource.get(position).getCategory());
holder.question.setText(mSource.get(position).getDescription());
//If no one of buttons isn't checked
holder.rbGroup.setOnCheckedChangeListener(null);
if (!mSource.get(position).isYesChecked() && !mSource.get(position).isNoChecked()) {
holder.rbGroup.clearCheck();
}else {
//We are supposing 'YES' is checkedint c = 0;
//Or 'NO' is checkedif (mSource.get(position).isNoChecked()){
c = 1;
}
//Set checked button
((RadioButton)holder.rbGroup.getChildAt(c)).setChecked(true);
}
holder.rbGroup.setOnCheckedChangeListener(listener);
return row;
}
}
Solution 2:
You can do like this override getItemId method and return your question id so you will able to identify each question and answer with id.
publicclassRadioAdapterextendsArrayAdapter<Question> {
List<Question> mSource;
staticclassViewHolder {
TextView category = null;
TextView question = null;
RadioGroup rbGroup = null;
ViewHolder(View row) {
this.category = (TextView) row.findViewById(R.id.tvQuestionCategory);
this.question = (TextView) row.findViewById(R.id.tvQuestion);
this.rbGroup = (RadioGroup) row.findViewById(R.id.rgIteration);
}
}
private LayoutInflater mInflater;
publicRadioAdapter(Context context, List<Question> mSource) {
super(context, R.layout.item_question, mSource);
mInflater = LayoutInflater.from(context);
this.mSource = mSource;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
View row = convertView;
if (row == null) {
row = mInflater.inflate(R.layout.item_question, null);
holder = new ViewHolder(row);
holder.rbGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
publicvoidonCheckedChanged(RadioGroup group, int checkedId) {
//Get position of current group//Integer pos = (Integer) group.getTag();switch (checkedId) {
case R.id.rbIterationYes:
//Check 'YES' and uncheck 'NO'
mSource.get((int) getItemId(position)).setYesChecked(true);
mSource.get((int) getItemId(position)).setNoChecked(false);
break;
case R.id.rbIterationNo:
//Vice versa
mSource.get((int) getItemId(position)).setNoChecked(true);
mSource.get((int) getItemId(position)).setYesChecked(false);
break;
}
}
});
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
holder.rbGroup.setTag(new Integer(position));
holder.category.setText(mSource.get(position).getCategory());
holder.question.setText(mSource.get(position).getDescription());
//If no one of buttons isn't checkedif (!mSource.get((int) getItemId(position)).isYesChecked() && !mSource.get((int) getItemId(position)).isNoChecked()) {
holder.rbGroup.clearCheck();
} else {
//We are supposing 'YES' is checkedint c = 0;
//Or 'NO' is checkedif (mSource.get((int) getItemId(position)).isNoChecked()) {
c = 1;
}
//Set checked button
((RadioButton) holder.rbGroup.getChildAt(c)).setChecked(true);
}
return row;
}
@Override
publiclonggetItemId(int position) {
return mSource.get(position).getId();
}
}
Post a Comment for "Radiogroup Doesnt Keep Properly State In Listview"