0

I'm making an android app that displays a game character based on the user's input/choices in the previous activity. I've used Extras to do this:

UserName = getIntent().getExtras().getSerializable("Name").toString();
UserCharacter = (String) getIntent().getExtras().getSerializable("Char").toString();
UserItem = getIntent().getExtras().getSerializable("Itemz").toString();
UserRating = getIntent().getExtras().getSerializable("Stars").toString();

I know this works because,

summary = (TextView) findViewById (R.id.Summary);
summary.setText("Name: " + UserName + "\nCharacter: " + UserCharacter + "\nItems: " + UserItem + "\nRating: " + UserRating);    

displays the user's selections completely fine. However, when I pass in these data into a if/else-if statement, the code simply ignores the if statements. Here's an example:

if (UserItem == "Wing + Helmet + Scarf")
image.setImageResource(R.drawable.tal);

Instead of displaying the new ImageView, the app would simply display the default image I had to use to create the ImageView in the .xml. I've tried various ways to work around this to no success. I know the ImageResource part works because if it is taken out of the if statements, then it changes the ImageView perfectly fine. Any suggestions would be greatly appreciated. Thanks in advance!

1 Answers1

2
if (UserItem == "Wing + Helmet + Scarf")
   image.setImageResource(R.drawable.tal);

Your condition always return false because you are comparing references not values. You have to use equals() method:

if (UserItem.equals("Wing + Helmet + Scarf"))
   image.setImageResource(R.drawable.tal);

Now it should works.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • 2
    Honestly, why do you always answer these duplicate questions? – A--C Mar 16 '13 at 20:10
  • @A--C honestly? i like helping, seriously. Question was placed and author want reply not link because is too lazy to find out it himself. I know that it's duplicate and on SO are "hundreds" of similar questions but how i said, author is always lazy. – Simon Dorociak Mar 16 '13 at 20:14
  • @A--C and i really like you! you are pretty good. – Simon Dorociak Mar 16 '13 at 20:16
  • I like helping too, which is why I commented with the solution ;-) But yeah, majority of authors are lazy :\ – A--C Mar 16 '13 at 20:23
  • 2
    There are three problems with answering dupes. 1) It reinforces bad behaviour 2) it reduces the signal to noise ratio which, of late, is getting pretty poor and 3) it encourages rep harvesting – Simon Mar 16 '13 at 20:26
  • Sorry about the duplicate question. I should have searched the website more thoroughly. Thank you for helping out nonetheless... Hopefully I won't become/or will be on my way out of being one of those "dupes" – user2152284 Mar 16 '13 at 20:48
  • 1
    @A--C and Simon - ok guys you blew me away now :) i'll be better :) – Simon Dorociak Mar 16 '13 at 20:57