我有列表视图,它的每一行都有一个radion按钮,我想只选择其中一行。
我的问题是我可以选择多个项目,并且即使我再次按下它时仍然打开了radion按钮,实际上我在三个月前遇到了这个问题并且我解决了它,现在我尝试了相同的解决方案但它不幸地没有& #39;工作
我的适配器代码
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;
答案 0 :(得分:0)
我找到了自己的解决方案,就像这样;
lv_allRestaurants.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View vuew,
int position, long id) {
// TODO Auto-generated method stub
if (adapterAll2.getPositionNowSelected() == position) {
adapterAll2.setPositionNowSelected(-1);
adapterAll2.ll_CancelDone.setVisibility(View.GONE);
} else {
adapterAll2.setPositionNowSelected(position);
adapterAll2.ll_CancelDone.setVisibility(View.VISIBLE);
}
adapterAll2.notifyDataSetChanged();
}
});
然后适配器是:
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) {
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 position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (vi == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
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);
tv_name.setText(restaurants.get(position).getName());
iv_image.setImageResource(restaurants.get(position).getImage());
rb_selected.setChecked(position == positionNowSelected);
return vi;
}
public int getPositionNowSelected() {
return this.positionNowSelected;
}
public void setPositionNowSelected(int now) {
this.positionNowSelected = now;
}
}