title: Values from checkboxes not working in Android
body: <p>I have made a test app. When one or more checkboxes are selected and a button is clicked, the numbers corresponding to the checkboxes should get added up and displayed in a textView (which initially displays 0).</p>
  <p><strong>MainActivity.java :</strong></p>
  <pre><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main2);
  
          CheckBox rice = findViewById(R.id.rice);
          boolean ateRice = rice.isChecked();
  
          CheckBox egg = findViewById(R.id.egg);
          boolean ateEgg = egg.isChecked();
  
          CheckBox bread = findViewById(R.id.bread);
          boolean ateBread = bread.isChecked();
  
          int cal = countCal(ateRice, ateEgg, ateBread);
          String calString = String.valueOf(cal);
  
          Button calculate = findViewById(R.id.calculate);
  
          calculate.setOnClickListener(view -&gt; {
              TextView numCal = findViewById(R.id.cal);
              numCal.setText(calString);
          });
  
      } //onCreate
  
      private int countCal(boolean ateRice, boolean ateEgg, boolean ateBread) {
          int calories = 0;
          if(ateRice){
              calories = calories + 20;
          }
          if(ateEgg){
              calories = calories + 50;
          }
          if(ateBread){
              calories = calories + 10;
          }
          return calories;
      } //countCal
  
  }
  </code></pre>
  <p>But when I click the button, nothing happens and the textView keeps displaying 0. What should I do?</p>
  <p><a href="https://i.stack.imgur.com/WKAwO.png" rel="nofollow noreferrer">(Here's how the app looks)</a></p>