2

I am working on android. I have a checkbox in my app and I want to save a custom text against my checkbox when it is checked or not.

CheckBox atbSealedCheckBox;
String selectedAtbSealedValue ="";
atbSealedCheckBox = (CheckBox) view.findViewById(R.id.atbCheckBox);

Now, If I checked the checkbox then I want to store Yes otherwise No. Also, I want to store it in my DB. For this I have already created get and set methods in my Model

private String atbSealed;

public String getAtbSealed(){return atbSealed;}

public void setAtbSealed(String atbSealed){this.atbSealed=atbSealed;}

Update 1

As per suggestion, I have tried below

atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

            if(isatbSealedChecked)
            {
                selectedAtbSealedValue = "Yes";
                ctQuantitySpinner.setEnabled(false);
                ctQuantitySpinner.setSelection(0);
                isatbSealedChecked = true;
            }
            else
            {
                selectedAtbSealedValue = "No";
                ctQuantitySpinner.setEnabled(true);
                ctQuantitySpinner.setSelection(0);
                isatbSealedChecked = false;
            }

        }
    });

But It doesn't help me out. Also, the ctQuantitySpinner is not disabled on atbSealedCheckbox checked. Also if checked the boolean value of the checkbox is not changed to true. Although in log I do see the selectedAtbSealedValue but it's not set in the app. In my SaveDataLocal() function I have set the value like below

 survey.setAtbSealed(this.selectedAtbSealedValue);
 SurveyManager dbHelper = new SurveyManager(getActivity());
    dbHelper.addSurvey(survey);

I have also tried the below code

 atbSealedCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(((CheckBox)v).isChecked())
            {
                ctQuantitySpinner.setEnabled(false);
                ctQuantitySpinner.setSelection(0);

                selectedAtbSealedValue = "Yes";
                isatbSealedChecked = true;
                //atbCheckBoxEdittext.setText(selectedAtbCheckBox);
            }
            else {
                ctQuantitySpinner.setEnabled(true);
                ctQuantitySpinner.setSelection(0);
                selectedAtbSealedValue = "No";
                isatbSealedChecked = false;
                //atbCheckBoxEdittext.setText(selectedAtbCheckBox);
            }
        }
    });

This code shows me the selected checkbox value, disabled the spinner and set the spinner value to null. but again the checkbox value is not saved.

How can I save custom text in a string?

3 Answers 3

1

Try this, it work for me:

atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
            if (isChecked) {
               selectedAtbSealedValue = "yes";
            } else {
               selectedAtbSealedValue = "no";
            }
        }
    } );

After that, you can store selectedAtbSealedValue into DB;

Update: Your fixed code:

atbSealedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {

        if(b)
        {
            selectedAtbSealedValue = "Yes";
            ctQuantitySpinner.setEnabled(false);
            ctQuantitySpinner.setSelection(0);
            isatbSealedChecked = true;
        }
        else
        {
            selectedAtbSealedValue = "No";
            ctQuantitySpinner.setEnabled(true);
            ctQuantitySpinner.setSelection(0);
            isatbSealedChecked = false;
        }

    }
});

Sign up to request clarification or add additional context in comments.

11 Comments

You try Log for value of selectedAtbSealedValue
Yes it's empty in either case
sure i'll add it
Why you don't use boolean b, b = true is checked, b=false is unchecked
if(b) { selectedAtbSealedValue = "Yes"; ctQuantitySpinner.setEnabled(false); ctQuantitySpinner.setSelection(0); isatbSealedChecked = true; } else { selectedAtbSealedValue = "No"; ctQuantitySpinner.setEnabled(true); ctQuantitySpinner.setSelection(0); isatbSealedChecked = false; }
|
0

As long as I understand your question, you want to change this variable String selectedAtbSealedValue =""; when user checked your checkbox.

To achieve this statement, you can use onCheckedListener that is triggered as soon as users checked or removed the check from your checkbox.

atbSealedCheckBox.setOnCheckedChangeListener( new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged( CompoundButton buttonView, boolean isChecked ) {
                // Todo something.
            }
        } );

Comments

0

This will help you. At submit button get the current value of checkbox.

 submit_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            control_of_vectors_value = control_of_vectors_checkbox.isChecked() == true ? "yes" : "no";  }}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.