0

I'm trying to display an image of an activity but I have a couple to choose from. So I used an if statement to be able to tell which to display based on a string, my problem is the string was retrieved from the first activity.

this is the code for sending the Strings to the other activity(part of the code);

        details.putExtra("des", des);
        details.putExtra("name", name);
        startActivity(details);

and these are the codes for retrieving and setting the images in Image View(part of the code);

    username.setText(intent.getStringExtra("name"));
    description.setText(intent.getStringExtra("des"));

    if (intent.getStringExtra("name").equals("Sheilla")) {
        image.setImageResource(R.drawable.sis);
    }

The image doesn't get displayed if I do this but does get displayed if I try something like

    if(true){
        image.setImageResource(R.drawable.sis);
    }

Can someone please tell me what I'm doing wrong and suggest a better way.

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
renegade
  • 13
  • 1
  • 4
  • Have you put a log to check the value of `name` in the first bit of code? – codeMagic Jan 29 '18 at 20:49
  • Yes it would appear that the value of "name" is not what you think it is. Put a log statement right before the if statement to check the value of "name" – Devsil Jan 29 '18 at 20:50
  • The below answer seem good, but ill also suggest try using `equalsIgnoreCase` method to check the name Sheilla... – Aalap Patel Jan 29 '18 at 20:56
  • Actually, its the same because there is textview on the second activity that makes use of the name and it displays "Sheilla". – renegade Jan 29 '18 at 21:01
  • 2
    Just for grins try this `String s = intent.getStringExtra("name").trim();` then `boolean b = s.equalsIgnoreCase("Sheilla");` and see what happens when you now try `b` in your `if` statement. – Barns Jan 29 '18 at 21:09
  • From where do you try to read the intent from, from onCreate or from onNewIntent? – Anton Makov Jan 29 '18 at 21:33

2 Answers2

0

Use this

        Intent intent = getIntent();

Then read values

if (intent.getStringExtra("name").equals("Sheilla")) {
    image.setImageResource(R.drawable.sis);
}

Try reading values

Justcurious
  • 2,201
  • 1
  • 21
  • 36
-1

Have you tried this?

    if (intent.getStringExtra("name").trim().equalsIgnoreCase("Sheilla") || intent.getStringExtra("name").trim() == "Sheilla") {
       image.setImageResource(R.drawable.sis); 
     }

this code adds some extra validation for the if statements, because sometimes in Java equals works and sometimes doesn't. So, you have to put 2 conditions like equals and ==

  • Not only is this really poor programing style (2x `intent.getStringExtra("name").trim()` !!) but also it is just wrong. It is widely known that you should never compare Java `String` with `==`. https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java :: https://stackoverflow.com/questions/17443201/why-doesn-t-work-on-string – Barns Jan 30 '18 at 15:29