mercredi 2 mars 2016

how to add compound listener to checkbox in android

I've tried to find answers online but I can't understand them fully or implement them. I would greatly appreciate your help. If you can please include an explanation. The problem is that the output always sets the text to 0.

package xyz.ashraf.whoisdelasalle;

import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;

/**
 * Created by Ashraf on 3/2/2016.
 */
public class check_Button extends Pop_sallian{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popwindow_sallian);
        CheckBox concern = (CheckBox) findViewById(R.id.concern);
        CheckBox faith = (CheckBox) findViewById(R.id.faith);
        CheckBox respect = (CheckBox) findViewById(R.id.respect);
        CheckBox education = (CheckBox) findViewById(R.id.education);
        CheckBox community = (CheckBox) findViewById(R.id.community);


        TextView output = (TextView) findViewById(R.id.output);

        int con = 0;
        int fai = 0;
        int res = 0;
        int edu = 0;
        int com = 0;

        if (concern.isChecked()) {
            con = con+1;
        }
        if (faith.isChecked()) {
            fai = fai+1;
        }
        if (respect.isChecked()) {
            res = res+1;
        }
        if (education.isChecked()) {
            edu = edu+1;
        }
        if (community.isChecked()) {
            com = com +1;
        }
        int score = con+fai+res+edu+com;

        if(score == 0){
            output.setText("score of 0");
        } else if(score == 1){
            output.setText("score of 1");
        } else if(score == 2){
            output.setText("score of 2");
        } else if(score == 3){
            output.setText("score of 3");
        } else if(score == 4){
            output.setText("score of 4");
        } else if(score == 5){
            output.setText("score of 5");
        } else{
            output.setText("Unknown");
        }
    }
}




Aucun commentaire:

Enregistrer un commentaire