Jey-Es
Jey-Es

Reputation: 53

CheckBox.isChecked() not returning expected value

I am a beginner in Android Development. I just want to seek for help about a problem that I encountered. The problem is shown in the image below. The application displays the wrong Toast text wherein the expected Toast message is:
Dog: True Cat: False Cow: False
But the actual Toast message is:
Dog: True Cat: True Cow: False

Image

Below is my MainActivity Code:

package com.example.johnsethsalazar.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private CheckBox c1, c2, c3; // Declaring Checkbox Variables.
    private Button select;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
        AddListenerToCheckBox1();
    }

    public void addListenerOnButton()
    {
        c1 = (CheckBox) findViewById(R.id.checkBox);
        c2 = (CheckBox) findViewById(R.id.checkBox2);
        c3 = (CheckBox) findViewById(R.id.checkBox3);
        select = (Button) findViewById(R.id.button);

        select.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                StringBuffer result = new StringBuffer();


                result.append("Dog : ").append(c1.isChecked());//Gives True if Check and False if Not.
                result.append("Cat : ").append(c2.isChecked());
                result.append("Cow : ").append(c3.isChecked());

                Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();

            }
        });
    }

    public void AddListenerToCheckBox1()
    {
        c2 = (CheckBox) findViewById(R.id.checkBox);
        c2.setOnClickListener(new View.OnClickListener()//Shows Toast("Dog is Selected") when Checkbox is Clicked.
        {
            @Override
            public void onClick(View view)
            {
                if (((CheckBox) view).isChecked())
                {
                    Toast.makeText(MainActivity.this, "Dog is Selected.", Toast.LENGTH_LONG).show();
                }
            }
        });
    }
}

Upvotes: 0

Views: 102

Answers (2)

jantursky
jantursky

Reputation: 1130

Fixed solution:

package com.example.johnsethsalazar.checkbox;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private CheckBox checkBoxDog, checkBoxCat, checkBoxCow;
    private Button select;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
        addListenerOnButton();
        AddListenerToCheckBox1();
    }

    public void findViews() {
        checkBoxDog = (CheckBox) findViewById(R.id.checkBox);
        checkBoxCat = (CheckBox) findViewById(R.id.checkBox2);
        checkBoxCow = (CheckBox) findViewById(R.id.checkBox3);
        select = (Button) findViewById(R.id.button);
    }

    public void addListenerOnButton() {
        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                printState();
            }
        });
    }

    public void printState() {
        StringBuilder result = new StringBuilder();
        result.append("Dog : ").append(checkBoxDog.isChecked());//Gives True if Check and False if Not.
        result.append("Cat : ").append(checkBoxCat.isChecked());
        result.append("Cow : ").append(checkBoxCow.isChecked());
        Toast.makeText(MainActivity.this, result.toString(), Toast.LENGTH_LONG).show();
    }

    public void AddListenerToCheckBox1() {
        checkBoxDog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(MainActivity.this, "Dog is Selected.", Toast.LENGTH_LONG).show();   
                }
            }
        });
    }
}

Upvotes: 0

lbenedetto
lbenedetto

Reputation: 2114

This is because you first do

    c1 = (CheckBox) findViewById(R.id.checkBox);
    c2 = (CheckBox) findViewById(R.id.checkBox2);
    c3 = (CheckBox) findViewById(R.id.checkBox3);

in your addListenerOnButton() which is correct, but then you call AddListenerToCheckBox1() which does

c2 = (CheckBox) findViewById(R.id.checkBox);

Overwriting the value of c2 with the dog check box.

I recommend naming your checkboxes more human friendly names so you don't get them mixed up.

Naming conventions would suggest checkBoxDog, checkBoxCat, ect

Upvotes: 2

Related Questions