I have a UI fragment using a custom listview with custom adapter. The listview is built with custom items containing a RadioButton.
So I overriden the getView() method like that:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = null;
if ((convertView == null)) {
LayoutInflater inflator = context.getLayoutInflater();
final ViewHolder viewHolder = new ViewHolder();
viewHolder.radioButton.setTag(list.get(position));
viewHolder.radioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(v.getTag() != site.getmSelectedPosition()){
if (site.getmSelectedRB() != null) {
Log.v(Constants.APP_TAG, "prev radio before " + site.getmSelectedRB().toString() + (site.getmSelectedRB().isChecked() ? " 1": " 0"));
site.getmSelectedRB().setChecked(false);
Log.v(Constants.APP_TAG, "prev radio after " + site.getmSelectedRB().toString() + (site.getmSelectedRB().isChecked() ? " 1": " 0"));
site.getmSelectedPosition().setSelected(false);
}
Log.v(Constants.APP_TAG, "new radio " + v.toString());
site.setmSelectedPosition((SiteInfo)v.getTag());
site.setmSelectedRB((RadioButton)v);
site.getmSelectedPosition().setSelected(true);
}
}
});
}
view.setTag(viewHolder);
} else {
view = convertView;
if (((ViewHolder) view.getTag()).radioButton != null)
((ViewHolder) view.getTag()).radioButton.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
if (holder.radioButton != null)
holder.radioButton.setChecked(list.get(position).isSelected());
return view;
}
So everything works fine in my list.
If the application goes in background or the device locks, everything works fine after coming back.
No, if I switch to another fragment then come back to this one, the list is correctly rebuilt thanks to the last lines just before return, but I get a problem if I click on a not checked RadioButton. I verified that site.getmSelectedRB() still contains the previous checked RadioButton.
So the first Log.v() displays 1 and after the call to site.getmSelectedRB().setChecked(false), it displays 0.
When I exit from this function the just clicked RadioButton is checked but also the previous one, which has been checked to false!
If I scroll the list, I see that the previous button state is correct, it is just not refreshed in fact. I guess the checked 'true' RadioButton is refreshed thanks to the click but what cause the checked 'false' RadioButton to be most of the time updated and not in this particular case?
Regards,
0 comments:
Post a Comment