在MainActivity
ListView
我有Checkboxes
自定义单选Adapter
每个项目都有一个复选框,它们被设置为List by Custom Adapter类。如果用户选中了一个,则弹出带有选择确认的对话框,接受后,应禁用所有复选框,但现在只禁用选定行中的一个。如何禁用所有并阻止用户更改选择的可能性?
public class CustomAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {
SparseBooleanArray mCheckStates;
private List<CustomClass> customList;
private Context context;
private LayoutInflater inflater;
private int selectedPosition = -1;
private Dialog dialog;
public CustomAdapter(Context _context, List<CustomClass> _customList) {
inflater = LayoutInflater.from(_context);
this.customList = _CustomList;
this.context = _context;
mCheckStates = new SparseBooleanArray(customList.size());
}
@Override
public int getCount() {
return customList.size();
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
CustomClass customClass = customList.get(i);
if (view == null)
view = inflater.inflate(R.layout.custom_list, null);
TextView name = (TextView) view.findViewById(R.id.tv_name);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox);
checkBox.setTag(i);
checkBox.setChecked(mCheckStates.get(i, false));
checkBox.setOnCheckedChangeListener(this);
checkBox.setChecked(false);
if (i == selectedPosition) {
checkBox.setChecked(true);
} else {
checkBox.setChecked(false);
}
checkBox.setOnClickListener(onStateChangedListener(checkBox, i));
name.setText(customClass.getName());
return view;
}
private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()) {
selectedPosition = position;
dialog = new Dialog(context);
dialog.setTitle("Accept");
dialog.setContentView(R.layout.dialog_accept);
dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
}
});
dialog.show();
Button btnAccept = (Button) dialog.findViewById(R.id.btnVote);
btnAccept.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
checkBox.setEnabled(false);
dialog.dismiss();
}
});
} else {
selectedPosition = -1;
}
notifyDataSetChanged();
}
};
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
notifyDataSetChanged();
}
public class CustomClass{
String name;
public CustomClass(){}
public CustomClass(String name){
this.name = name;
}
public String getName(){return name;}
public void setName(String name){this.name = name;}
}
CustomClass:
MainActivity
ListView
仅设置Adapter
和{{1}}
答案 0 :(得分:2)
包括组内的所有复选框,以便只能选中一个复选框。
答案 1 :(得分:0)
ListView list = (ListView) findViewById("R.id.list");
for(int x = 0; x < list.getItems.length(); x++){ list.items.get(x).setEnabled(false);}
不确定,但也许是这样的。值得一试?
答案 2 :(得分:0)
在Checkbox中更改侦听器
private int selectedPosition = 0;
private View.OnClickListener onStateChangedListener(final CheckBox checkBox, final int position) {
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkBox.isChecked()) {
customClass.setSelected(true);
customList.get(selectedPosition).setSelected(false);
selectedPosition = position;
}
notifyDataSetChanged();
}
};
Getview()
中的
if(customClass.getSelected()){
checkBox.setEnable(true);
}else{
checkBox.setEnable(false);
}