-1

How to display string values from this code

  TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);

 String allChecked = "", answer = "";
    Bundle bundle = getIntent().getExtras();

    if (bundle != null) {
        if (bundle.containsKey("KEY_ALL_CHECKED"))
            allChecked = bundle.getString("KEY_ALL_CHECKED");

        answer = bundle.getString("KEY_ANSWER");

in here

textViewDisplayResult.setText();

UPDATED POST (methods checkAnswers and allAnswerChecked from MainActivity)

 private boolean checkAnswers() {
    for (boolean radioAnswer : answer) {
        if (!radioAnswer) {
            return false;
        }
    }
    return true;
}

private boolean allAnswersChecked() {
    for (boolean radioAnswer : isAnswered) {
        if (!radioAnswer) {
            return false;
        }
    }
    return true;
}

And method buttonCheckAnswer

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

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

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



            startActivity(intent);

        }
    });

AnswerActivity.java

public class AnswerActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_answer);
    TextView textViewDisplayResult = (TextView) findViewById(R.id.text_view_display_result);


    Bundle bundle = getIntent().getExtras();
    if (bundle != null) {
        if (bundle.containsKey("KEY_ALL_CHECKED"))

            textViewDisplayResult.setText(bundle.getString("KEY_ALL_CHEC‌​KED") + " "+ bundle.getString("KEY_ANSWER"));
        }

    }

}

Ramona
  • 159
  • 1
  • 10

3 Answers3

1

You can set text directly using.

textViewDisplayResult.setText(bundle.getString("KEY_ALL_CHECKED") + " "+ bundle.getString("KEY_ANSWER"));
Upendra Shah
  • 2,218
  • 17
  • 27
  • Hi, using this code I get warnings `allChecked` and `answer` `never accessed` – Ramona May 11 '17 at 09:07
  • yes you have to remove line String allChecked = "", answer = ""; – Upendra Shah May 11 '17 at 09:23
  • also remove allChecked = bundle.getString("KEY_ALL_CHECKED"); answer = bundle.getString("KEY_ANSWER"); – Upendra Shah May 11 '17 at 09:29
  • at final Replace :=> **String allChecked = "", answer = ""; Bundle bundle = getIntent().getExtras(); if (bundle != null) { if (bundle.containsKey("KEY_ALL_CHECKED")) allChecked = bundle.getString("KEY_ALL_CHECKED"); answer = bundle.getString("KEY_ANSWER");** ======= with :=> **Bundle bundle = getIntent().getExtras(); if (bundle != null) { if (bundle.containsKey("KEY_ALL_CHECKED")) textViewDisplayResult.setText(bundle.getString("KEY_ALL_CHECKED") + " "+ bundle.getString("KEY_ANSWER"));** – Upendra Shah May 11 '17 at 09:32
  • Now I get `null` displayed :( – Ramona May 11 '17 at 09:40
  • Upendra, but where is value "KEY_ANSWER" in Bundle? We use it in `textViewDisplay....` but we don't have any `"KEY_ANSWER"` earlier in `Bundle` – Ramona May 11 '17 at 09:48
  • haven't you passed bundle from previous activity?? if you passed that than it will directly get value for KEY_ANSWER and KEY_ALL_CHEC‌​KED. – Upendra Shah May 11 '17 at 09:50
  • Updated my post with whole `AnswerActivity.java` for you to have a clear view on this issue. Or maybe you ask about sth else? – Ramona May 11 '17 at 09:54
  • you are using **R.string.Good_answer** it is an id so you have to write at there **getResources().getString(R.string.Good_answer)** and same for **R.string.text_not_checked** and **R.string.Wrong_answer** – Upendra Shah May 11 '17 at 10:16
  • Where exactly I have to write `getResources().getString...`? – Ramona May 11 '17 at 10:19
  • **intent.putExtra("KEY_ALL_CHECKED", R.string.text_not_checked);** replace with **intent.putExtra("KEY_ALL_CHECKED", getResources().getString(R.string.text_not_checked));** **intent.putExtra("KEY_ANSWER", R.string.Good_answer);** replace with **intent.putExtra("KEY_ANSWER", getResources().getString(R.string.Good_answer));** **intent.putExtra("KEY_ANSWER", R.string.Wrong_answer);** replace with **intent.putExtra("KEY_ANSWER", getResources().getString(R.string.Wrong_answer));** – Upendra Shah May 11 '17 at 10:24
  • Ok Upendra, I think we are almost there. Now when I run it I get `"null Wrong answer'` or `"null Good answer'` but I never get `"text not checked'` even when I should have. – Ramona May 11 '17 at 10:30
  • just try to pass hard coded string at replace of getResources().getString(R.string.text_not_checked) – Upendra Shah May 11 '17 at 10:38
  • Upendra, when debugging I get `savedInstanceState=null` in line `if (bundle !=null.....` ,maybe this is any help? – Ramona May 11 '17 at 10:39
  • I've changed to `" intent.putExtra("KEY_ALL_CHECKED", "text_not_checked");` but no luck :( – Ramona May 11 '17 at 10:42
  • please change variable "KEY_ALL_CHECKED" to anything else.. or copy and paste same variable name wherever you used – Upendra Shah May 12 '17 at 05:43
  • Hello Upendra. Going through my posts I came across your messages and thought maybe you could help me on this one https://stackoverflow.com/questions/43997655/zoom-in-out-the-whole-page-layout. Many thanks in advance. – Ramona May 16 '17 at 09:55
  • have you get solution of this question?? if yes than mark it as answer and upvote so other can get easily get solutions.. – Upendra Shah May 16 '17 at 10:35
0

Do you mean this?

String answer = bundle.getString("KEY_ANSWER");
textViewDisplayResult.setText(answer);
David Luque
  • 1,078
  • 5
  • 18
  • 30
0

You can set bunlde values in text view like this:

textViewDisplayResult.setText(answer);
textViewDisplayResult.setText(allChecked);
Qandil Tariq
  • 539
  • 1
  • 3
  • 15