javaandroid-studio

TextView returning both true and false statements instead of only true


Program built to give a choice between 3 favorite restaurants after clicking and submitting your favorite it will return a true or false on which one was selected. One issue I'm having is after submitting the program will return all three options but only the selected will return true. How would I go about only having the true statements to appear while the false being invisible? Any help would be appreciated!

public class MainActivity extends AppCompatActivity {

  private CheckBox BurgatoryCheckBox;
  private CheckBox SiennaCheckBox;
  private CheckBox BakersCheckBox;

  private Button submitButton;
  private TextView showTextView;

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

    BurgatoryCheckBox = (CheckBox) findViewById(R.id.checkBoxBurgatory);
    SiennaCheckBox = (CheckBox) findViewById(R.id.checkBoxSienna);
    BakersCheckBox = (CheckBox) findViewById(R.id.checkBoxBakers);

    showTextView = (TextView) findViewById(R.id.txtViewResults);

    submitButton = (Button) findViewById(R.id.btnSubmit);
    submitButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //BurgatoryCheckBox
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.append(BurgatoryCheckBox.getText().toString() +
                    "state is:"+ BurgatoryCheckBox.isChecked() + "\n");

            stringBuilder.append(SiennaCheckBox.getText().toString() +
                    "state is:"+ SiennaCheckBox.isChecked() + "\n");

            stringBuilder.append(BakersCheckBox.getText().toString() +
                    "state is:"+ BakersCheckBox.isChecked() + "\n");

            showTextView.setVisibility(View.VISIBLE);
            showTextView.setText(stringBuilder);

        }
    });
  }
}

Solution

  • How would I go about only having the true statements to appear while the false being invisible?

    The answer to your question lies in the power of the if-statement. You can use an if-statement to define the direction your code would follow based on conditions.

    In pseudocode, you can have something like this:

    if BurgatoryCheckBox.isChecked -> stringbuilder.append(...)
    if SiennaCheckBox.isChecked -> stringbuilder.append(...)
    if BakersCheckBox.isChecked -> stringbuilder.append(...)
    

    This way, if BurgatoryCheckBox is not checked, nothing will happen, no StringBuilder appending would take place. The same applies to SiennaCheckBox and BakersCheckBox.

    In actual Java code, your OnClick method should look like this:

    @Override
        public void onClick(View v) {
    
            StringBuilder stringBuilder = new StringBuilder();
    
            if (BurgatoryCheckBox.isChecked()) { 
              stringBuilder.append(BurgatoryCheckBox.getText().toString() +
                    "state is:"+ BurgatoryCheckBox.isChecked() + "\n");
            }
    
           if(SiennaCheckBox.isChecked()) {
              stringBuilder.append(SiennaCheckBox.getText().toString() +
                    "state is:"+ SiennaCheckBox.isChecked() + "\n");
            }
    
            if(BakersCheckBox.isChecked()) {
              stringBuilder.append(BakersCheckBox.getText().toString() +
                    "state is:"+ BakersCheckBox.isChecked() + "\n");
            }
    
            showTextView.setVisibility(View.VISIBLE);
            showTextView.setText(stringBuilder);
    
        }
    });