我有列表视图,它的每一行都有一个单选按钮,我只想选择其中一行。
我的问题是我可以选择多个项目并且即使我再次按下单选按钮仍然打开,实际上我三个月前遇到了这个问题并且我解决了它,现在我尝试了相同的解决方案但不幸的是它没有工作.
我的适配器代码
class AdapterRestaurantSelectOne extends BaseAdapter {
private ArrayList<Boolean> rb_status = new ArrayList<Boolean>();
private static LayoutInflater inflater = null;
private List<Restaurant> restaurants = null;
Context context;
LinearLayout ll_CancelDone;
public int positionNowSelected;
public AdapterRestaurantSelectOne(Context context,
List<Restaurant> restaurants, LinearLayout ll_CancelDone,
RadioGroup radioGroup) {
positionNowSelected = -1;
this.context = context;
this.restaurants = restaurants;
this.ll_CancelDone = ll_CancelDone;
inflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < restaurants.size(); i++) {
rb_status.add(false);
}
}
public Restaurant getRestaurant() {
return restaurants.get(positionNowSelected);
}
@Override
public int getCount() {
return restaurants.size();
}
@Override
public Object getItem(int position) {
return restaurants.get(position);
}
@Override
public long getItemId(int position) {
return restaurants.get(position).getID();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null)
vi = inflater.inflate(R.layout.profile_list_item_radiobutton, null);
TextView tv_name = (TextView) vi
.findViewById(R.id.tv_profile_list_item_radioButton_title);
ImageView iv_image = (ImageView) vi
.findViewById(R.id.iv_profile_list_item_radiobutton_image);
RadioButton rb_selected = (RadioButton) vi
.findViewById(R.id.cb_profile_list_item_radioButton_radioButton);
rb_selected.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
rb_status.set(position, isChecked);
boolean status = false;
int i = 0;
for (Boolean b : rb_status) {
if (b) {
status = true;
break;
}
i++;
}
if (status) {
Basket.setRestaurant(restaurants.get(i));
positionNowSelected = i;
ll_CancelDone.setVisibility(View.VISIBLE);
} else {
Basket.setRestaurant(null);
positionNowSelected = -1;
ll_CancelDone.setVisibility(View.GONE);
}
}
});
rb_selected.setChecked(rb_status.get(position));
// rb_selected.setChecked(position == positionNowSelected);
tv_name.setText(restaurants.get(position).getName());
iv_image.setImageResource(restaurants.get(position).getImage());
return vi;