Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner with CheckBox setChecked(true) not working

Tags:

android

if I try to set the checked state of a CheckBox it does not work, I have read many threads by others and all I could get is that its a different checkbox that is being returned every time but I'm setting this new checkbox so why its not being checked in my spinner?

 classesName.setAdapter(new SpinnerAdapter() {

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer) {


        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer) {


        }

        @Override
        public boolean isEmpty() {

            return false;
        }

        @Override
        public boolean hasStableIds() {

            return false;
        }

        @Override
        public int getViewTypeCount() {

            return 0;
        }

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {

            LayoutInflater inflater = Registeration.this.getLayoutInflater();
            View spinView = inflater.inflate(R.layout.classes_registeration_ddl, null);
                CheckBox rbtn = (CheckBox) spinView.findViewById(R.id.radioButtonClassesReg);
                    rbtn.setChecked(true);

return spinView;

        }


        @Override
        public int getItemViewType(int arg0) {

            return 0;
        }

        @Override
        public long getItemId(int arg0) {

            return arg0;
        }

        @Override
        public Object getItem(int arg0) {

            return classesArr[arg0];
        }

        @Override
        public int getCount() {

            return classesArr.length;
        }

        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = Registeration.this.getLayoutInflater();
            View spinView = inflater.inflate(R.layout.classes_registeration_ddl, null);
                CheckBox rbtn = (CheckBox) spinView.findViewById(R.id.radioButtonClassesReg);
                    rbtn.setChecked(true);
return spinView;
            }


});
like image 689
Mahmoud Hadad Avatar asked Nov 12 '22 15:11

Mahmoud Hadad


1 Answers

The reason for this is that android ListView's code runs after getDropDownView to keeps track of selected states. You can work around this with a custom CheckBox class which overrides isChecked and returns true/false based on the parents data.

like image 61
neuron Avatar answered Nov 15 '22 04:11

neuron