gpt4 book ai didi

java - 选择一个复选框后禁用 ListView 中的所有复选框

转载 作者:行者123 更新时间:2023-12-02 02:31:13 24 4
gpt4 key购买 nike

MainActivity中,我有ListView,带有自定义单选Checkboxes,每个项目都有一个复选框,并且它们通过自定义适配器类设置为列表。如果用户选中一项,则弹出对话框,其中包含选择确认,接受后所有复选框均应被禁用,但目前仅禁用所选行中的一个。如何禁用所有并阻止用户更改选择的可能性?

适配器

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;}

}

MainActivity只设置了ListViewAdapter

最佳答案

将所有复选框包含在一组中,以便只能选择一个复选框。

关于java - 选择一个复选框后禁用 ListView 中的所有复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47063163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com