Moeez
Moeez

Reputation: 478

How to set checkbox to a custom text in string and save into database?

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?

Upvotes: 2

Views: 199

Answers (3)

Nitin Sharma
Nitin Sharma

Reputation: 100

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";  }}

Upvotes: 0

Tung Tran
Tung Tran

Reputation: 2955

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;
        }

    }
});

Upvotes: 1

Olkunmustafa
Olkunmustafa

Reputation: 3203

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.
            }
        } );

Upvotes: 0

Related Questions