Reputation: 201
I have a ListView
with own adapter: CityZipCodesAdapter
. In the constructor I give a List<CityZipCodesDataModel>
where I keep String
, String
, and boolen
(the RadioButton
was selected or not). This i write in SharedPreferences
and then I read this. It all works.
public class CityZipCodesAdapter extends ArrayAdapter<CityZipCodesDataModel> {
private final List<CityZipCodesDataModel> list;
private final Activity context;
private RadioButton selectedRB;
private int selectedPosition = -1;
public CityZipCodesAdapter(Activity context,
List<CityZipCodesDataModel> list) {
super(context, R.layout.city_list_item, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView cityOrZipCode;
protected TextView listOfZipCodesInNear;
protected RadioButton radioBtn;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder = null;
if (view == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.city_list_item, null);
viewHolder = new ViewHolder();
viewHolder.cityOrZipCode = (TextView) view.findViewById(R.id.cityOrZipCode);
viewHolder.listOfZipCodesInNear = (TextView) view.findViewById(R.id.listOfZipCodesInNear);
viewHolder.radioBtn = (RadioButton)view.findViewById(R.id.radioButton);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag() ;
}
viewHolder.radioBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(position != selectedPosition && selectedRB != null){
selectedRB.setChecked(false);
}
selectedPosition = position;
selectedRB = (RadioButton)v;
}
});
if(selectedPosition != position){
viewHolder.radioBtn.setChecked(false);
}else{
viewHolder.radioBtn.setChecked(true);
if(selectedRB != null && viewHolder.radioBtn != selectedRB){
selectedRB = viewHolder.radioBtn;
}
}
viewHolder.listOfZipCodesInNear.setText(list.get(position).getZipCodes());
viewHolder.cityOrZipCode.setText(list.get(position).getLocation());
viewHolder.radioBtn.setText(list.get(position).getLocation());
viewHolder.radioBtn.setChecked(list.get(position).isSelectedLocation());
return view;
}
}
The problem is when i turn on the application then the correct RadioButton
is selected, but it is inactive. There is only displayed correctly.
For example,
x String1
o String2
o String3
When I click on String2 (String1 was inictialy selected) then String2 is selected, but String1 is also selected. When I click on String1, then String2 then the selection is correct.
Do you have any idea what is wrong? Many thanks for your help!
Upvotes: 0
Views: 356
Reputation: 1579
If you want just a single radio button to show the clickable state then you must manually remove others... what i mean is:
onclick...
radio2.setChecked(true);
radio1.setChecked(false);
radio3.setChecked(false);
otherwise you set whatever you want to set but you dont cancel the others... if they are all in the same position... it should be simple...
as far as changing other positions I dont think that what you did would work...i think after each click you need to rebuild your listview and for each position change... or use notifyDataSetChanged()
or something like that...
found this for you
Custom Listview with CheckBox single selection
think it will help...
Upvotes: 1
Reputation: 43023
I think the problem is in this part of the code:
if(selectedPosition != position){
viewHolder.radioBtn.setChecked(false);
}else{
viewHolder.radioBtn.setChecked(true);
if(selectedRB != null && viewHolder.radioBtn != selectedRB){
selectedRB = viewHolder.radioBtn;
}
}
Initially selectedPosition != position
so selectedRB
is never set and remains null. When you click the first radio button initially, it gets set and it works but if you don't, the code doesn't unselect it.
Upvotes: 1