ScottishUser ScottishUser - 1 month ago 11
Android Question

disable checkbox in a simple list view multi choice?

Is there a means to disable a checkbox (prevent the box from being clickable) on a simple list view multiple choice?

I'm simply defining a standard listview in my xml layout, setting as multi choice and when the user checks a box I want to disable/lock the selection (after an onClick).

Ive tried playing with: theCheckBox.setClickable(false);
but not sure how to get theCheckBox from my simple listview?

any ideas?

thanks.

My adapter is simple:

adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, this.ingredientArray);
itemList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
itemList.setAdapter(adapter);

Answer

You can override you getView in the anonymous ArrayAdapter class and apply the disabling of the view in it.

sample:

ArrayAdapter adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, this.ingredientArray){
        public View getView(int position, View convertView, ViewGroup parent) {
            final CheckedTextView ctv = (CheckedTextView)convertView.findViewById(android.R.id.text1);
            ctv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(ctv.isChecked())
                        ctv.setClickable(false);
                }
            });
        };
    };
    itemList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    itemList.setAdapter(adapter);

EDIT:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice, this.ingredientArray){
            public View getView(int position, View convertView, ViewGroup parent) {

                if(convertView == null)
                {
                    View v = getLayoutInflater().inflate(android.R.layout.simple_list_item_multiple_choice, null);

                    final CheckedTextView ctv = (CheckedTextView)v.findViewById(android.R.id.text1);
                    ctv.setText(ingredientArray[position]);

                    ctv.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            if(!ctv.isChecked())
                            {
                                ctv.setChecked(true);
                            }
                        }

                    });
                    return v;
                }

                return convertView;
            };
        };
Comments