1

I am using switch component inside my list item. But no matter I enable the switch or not. It's returning false.

  group_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (group_select.isChecked()){
                        Toast.makeText(context, "Checked at "+position, Toast.LENGTH_SHORT).show();
                    }
                    else if (!group_select.isChecked()){
                        Toast.makeText(context, "Unchecked at "+position, Toast.LENGTH_SHORT).show();
                    }
                }
        });

Thanx for the help....

Doge
  • 853
  • 11
  • 35
  • It would help if you said what class group_select is an instance of. – alzee Jan 21 '17 at 18:04
  • @Doge post your full code – Charuක Jan 21 '17 at 18:10
  • wait! It should work unless you recreate the view again are you using setContentView again? – Charuක Jan 21 '17 at 18:17
  • I changed it to setOnCheckedChangeListener and it worked @Charuka Thanx :) – Doge Jan 21 '17 at 18:22
  • @Doge as you wish but you didn't understood the real problem setOnClickListener works too unless you make a mistake anyways good luck – Charuක Jan 21 '17 at 18:23
  • I am using setOnclickListener in the getView method of adapter @Charuka . – Doge Jan 21 '17 at 18:26
  • `if (c) {...} else if (!c) {...}` is logically equivalent to `if (c) {...} else {...}` modulo side effects of `c`. If there were side effects then the first idiom is an invitation to bugs. `group_select` doesn't follow the naming conventions. – Lew Bloch Jan 21 '17 at 18:42
  • I know I made else if to check that if it's showing the check value false or there is any different reason..After that I changed again – Doge Jan 21 '17 at 19:06

2 Answers2

2
mSubscriptionSw.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                                         boolean isChecked) {
                if (isChecked) {
                    // checked
                } else {
                    // not checked
                }
            }
        });
Charuක
  • 12,953
  • 5
  • 50
  • 88
2
ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton); 
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                // The toggle is enabled
            } else {
                // The toggle is disabled
            }
        } });
Mohammad Salem
  • 1,113
  • 9
  • 15