Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 选中一个复选框后,禁用ListView中的所有复选框_Java_Android_Listview - Fatal编程技术网

Java 选中一个复选框后,禁用ListView中的所有复选框

Java 选中一个复选框后,禁用ListView中的所有复选框,java,android,listview,Java,Android,Listview,在MainActivity中,我有ListView和自定义单选复选框每个项目都有一个复选框,它们被设置为按自定义适配器类列出。若用户选中了一个复选框,则应禁用带有选择确认和接受后所有复选框的弹出对话框,但目前仅禁用所选行中的一个复选框。如何禁用所有和阻止用户更改选择的可能性 适配器 public class CustomAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener { Spa

MainActivity
中,我有
ListView
和自定义单选
复选框
每个项目都有一个复选框,它们被设置为按自定义适配器类列出。若用户选中了一个复选框,则应禁用带有选择确认和接受后所有复选框的弹出对话框,但目前仅禁用所选行中的一个复选框。如何禁用所有和阻止用户更改选择的可能性

适配器

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();


    }
ListView list = (ListView) findViewById("R.id.list");
for(int x = 0; x < list.getItems.length(); x++){ list.items.get(x).setEnabled(false);}
MainActivity
只设置了
ListView
Adapter

ListView list=(ListView)findViewById(“R.id.list”);
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();


    }
ListView list = (ListView) findViewById("R.id.list");
for(int x = 0; x < list.getItems.length(); x++){ list.items.get(x).setEnabled(false);}
对于(int x=0;x
不确定,但可能是这样的。值得一试吗?

ListView list=(ListView)findViewById(“R.id.list”);
对于(int x=0;x

不确定,但可能是这样的。值得一试吗?

在复选框更改侦听器中

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()中


在复选框中更改侦听器

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()中


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

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