0

Since getBooleanExtra method has two values - true and false, how to write code using getBooleanExtra to display two different string values?

For example pressing RadioButton1 should display string from getBooleanExtra 1 and pressing RadioButton2 should display string 2.

I wrote something like this but it isn't doing the job.

textViewDisplayResult.setText(getIntent().getBooleanExtra("KEY_ANSWER", false)?getString(R.string.1):getString(R.2));

POST UPDATE

so how to pass values to other activity using Bundle in below case?

 final Intent intent = new Intent(MainActivity.this, AnswerActivity.class);

    buttonCheckAnswer.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!allAnswersChecked())
                intent.putExtra("KEY_ALL_CHECKED", R.string.text_not_checked);

            else if (checkAnswers())
                intent.putExtra("KEY_ANSWER", R.string.Good_answer);

            else
                intent.putExtra("KEY_ANSWER", R.string.Wrong_answer);

            startActivity(intent);

        }
    });
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Ramona
  • 159
  • 1
  • 10
  • 1
    You try compile it or made that up? Its not even compile `getString(R.2)`. It's not have two arguments true and false..It has 2 arguments `name` and default boolean value, which is true/false. Make variable boolean of your "KEY_ANSWER" value and then in if clause set proper value. – miljon May 09 '17 at 09:48
  • Hi, I didn't make it up. I just can not make it working. Can you please give me example of what you suggest me to do? – Ramona May 09 '17 at 09:54
  • I've corrected my post. Thank you miljon. – Ramona May 09 '17 at 10:04

1 Answers1

1

"Since getBooleanExtra method has two arguments - true and false"... This is wrong. getBooleanExtra() has two arguments: one is Your key of the passed boolean value and other is the default value. So you can not pass two values using single putBooleanExtra().

You can use a Bundle to pass more than one values. See this for mor info.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72
  • I've corrected my post. Thank you Kaushal. So if let's say I have `buttonCheckAnswer` with `if-else` statement, then the `Bundle` is the only way to display `Keys` in other `activity`? – Ramona May 09 '17 at 10:07
  • I said you can use bundle to send more then one values. So if you want to send two boolean variables, then put them in bundle and send them. – Kaushal28 May 09 '17 at 10:10
  • Thank you. I have one more question for you Kaushal. Please have a look at my updated post. – Ramona May 09 '17 at 10:28
  • http://stackoverflow.com/questions/4999991/what-is-a-bundle-in-an-android-application – Kaushal28 May 09 '17 at 10:37
  • Ok, but how to get `boolean` values into `Bundle` from both `"KEY_ANSWER"` which is `"Good answer"` and `"Wrong answer"`? – Ramona May 09 '17 at 10:59