Hi I’m developing with new android 4.0 SDK on Eclipse.
Somehow this simple if statement is not working.
I get data from another activity through a bundle:
Bundle gotBundle = getIntent().getExtras();
gotPack = gotBundle.getString("key");
Toast.makeText(this, gotPack, Toast.LENGTH_SHORT).show();
i’ve tried outputting ‘gotPack’ on a toast and alert dialogue box, both of which return a string value ‘Pattern 1’. (which is what i wanted so far)
Now when i try my if statement:
if (gotPack == "Pattern 1") {
filePath="android.resource://" + getPackageName() + "/" + R.raw.p2;
Toast.makeText(this, "WORKED1", Toast.LENGTH_SHORT).show();
}
it refuses to enter into the if statement, i have tested this as it does not produce the toast output or any other results. What can i be doing wrong? gotPack is initially identified as a string.
For String comparison you need to use
.equals()if ("Pattern 1".equals(gotPack)) {Java Objects are typically started with a capitol letter (*O*bject, *S*tring, *L*ist, *I*nteger) while primatives are lowercase (*i*nt, *d*ouble). Any object must override the .equals() method in order to do a proper comparison of the objects otherwise you’re comparing the memory reference and not the object itself.