I would like a TextView to display the number of CheckBoxes that have been marked

0

I made several attempts, but I did not succeed, here is the code from MainActivity.java:

package genesysgeneration.cbnumber;

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

public class MainActivity extends AppCompatActivity {

    CheckBox cb_01, cb_02, cb_03, cb_04, cb_05, cb_06, cb_07, cb_08;
    int cont = 0;
    TextView tvContador;

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

        cb_01=(CheckBox)findViewById(R.id.checkBox1);
        cb_02=(CheckBox)findViewById(R.id.checkBox2);
        cb_03=(CheckBox)findViewById(R.id.checkBox3);
        cb_04=(CheckBox)findViewById(R.id.checkBox4);
        cb_05=(CheckBox)findViewById(R.id.checkBox5);
        cb_06=(CheckBox)findViewById(R.id.checkBox6);
        cb_07=(CheckBox)findViewById(R.id.checkBox7);
        cb_08=(CheckBox)findViewById(R.id.checkBox8);

        tvContador=(TextView)findViewById(R.id.tvContador);
        tvContador.setText(String.valueOf(cont));

    }

    public void onCheckboxClicked (View view){

        boolean checked = ((CheckBox) view).isChecked();
        switch (view.getId()){

            case R.id.checkBox1:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox2:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox3:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox4:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox5:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox6:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox7:
                if(checked)
                    cont+=1;
                break;

            case R.id.checkBox8:
                if(checked)
                    cont+=1;
                break;

        }

    }

}

What happens

Here'swhatI'dliketoseehappenexactly.

    
asked by anonymous 04.01.2017 / 00:00

3 answers

2

Friend, let me give you a tip, you can try to simplify this code. Since they are all checkboxes and they are basically the same view. Try to make a single method that will count and update the TextView, and call it in the checkbox. This will reduce your code and will facilitate future fixes and improvements. Try this:

public class Teste extends Activity implements View.OnClickListener{

    CheckBox cb_01, cb_02, cb_03, cb_04, cb_05, cb_06, cb_07, cb_08;
    int cont = 0;
    TextView tvContador;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.teste);

        cb_01=(CheckBox)findViewById(R.id.checkBox1);
        cb_02=(CheckBox)findViewById(R.id.checkBox2);
        cb_03=(CheckBox)findViewById(R.id.checkBox3);
        cb_04=(CheckBox)findViewById(R.id.checkBox4);
        cb_05=(CheckBox)findViewById(R.id.checkBox5);
        cb_06=(CheckBox)findViewById(R.id.checkBox6);
        cb_07=(CheckBox)findViewById(R.id.checkBox7);
        cb_08=(CheckBox)findViewById(R.id.checkBox8);
        tvContador=(TextView)findViewById(R.id.tvContador);

        cb_01.setOnClickListener(this);
        cb_02.setOnClickListener(this);
        cb_03.setOnClickListener(this);
        cb_04.setOnClickListener(this);
        cb_05.setOnClickListener(this);
        cb_06.setOnClickListener(this);
        cb_07.setOnClickListener(this);
        cb_08.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        if (((CheckBox)v).isChecked()) {

            cont += 1;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

        } else {

            cont -= 1;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

        }
    }
}
    
10.02.2017 / 16:43
3

If the reference to each CheckBox is only needed to assign the listener , you can simplify the code by assigning the listener to the xml .

activity_main.xml

...
<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox1" 
    ...
    android:onClick="onCheckBoxClick"/>

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/checkBox2" 
    ...
    android:onClick="onCheckBoxClick"/>

...

MainActivity.java

public class MainActivity extends AppCompatActivity{

    int cont = 0;
    TextView tvContador;

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

    public void onCheckBoxClick(View view) {
        if (((CheckBox)v).isChecked()) {
            cont++;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));
       else{
            cont--;
            tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));
       }
    }
}
    
10.02.2017 / 16:55
0

I got the solution shortly after the question, but I looked and saw that no one had yet answered it. I decided to show myself how I resolved.

Follow the code:

package genesysgeneration.cbnumber;

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

public class MainActivity extends AppCompatActivity {

    CheckBox cb_01, cb_02, cb_03, cb_04, cb_05, cb_06, cb_07, cb_08;
    int cont = 0;
    TextView tvContador;

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

        cb_01=(CheckBox)findViewById(R.id.checkBox1);
        cb_02=(CheckBox)findViewById(R.id.checkBox2);
        cb_03=(CheckBox)findViewById(R.id.checkBox3);
        cb_04=(CheckBox)findViewById(R.id.checkBox4);
        cb_05=(CheckBox)findViewById(R.id.checkBox5);
        cb_06=(CheckBox)findViewById(R.id.checkBox6);
        cb_07=(CheckBox)findViewById(R.id.checkBox7);
        cb_08=(CheckBox)findViewById(R.id.checkBox8);
        tvContador=(TextView)findViewById(R.id.tvContador);

        addCheck();

    }

    private void addCheck() {

        cb_01.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()) {

                    cont += 1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                } else {

                    cont -= 1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_02.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_03.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_04.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_05.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_06.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_07.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

        cb_08.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (((CheckBox)v).isChecked()){

                    cont+=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }else {

                    cont-=1;
                    tvContador.setText(String.valueOf(cont + " de 8 foram marcados."));

                }

            }
        });

    }

}
    
10.02.2017 / 01:22