0

I am passing a string from one activity to the other. I pass the text "maglist". When i log the string in the next activty it says "maglist" as it should do. BUT, if i do an if statement which asks if the string = "maglist" then is just goes to the else when it should be true. Il show you:

First Activity:

        Intent intentRefresh = new Intent(getApplicationContext(), com.fieldandrurallife.media.InternetRefresh.class);
        String list = "maglist";
        intentRefresh.putExtra("from", list);
        intentRefresh.putExtra("cat", cat);
        startActivity(intentRefresh);  

Second Activity:

    Intent iB = getIntent();        
    String from = iB.getStringExtra("from");
    if(from == "maglist"){
        Log.d("refresh", "From = " + from);
    } else {
        Log.d("refresh", "dident work" + from);
    }

I get the LOG of "dident work maglist" when it should be "maglist" Confused. Thanks in advanced.

Tom Doe
  • 331
  • 6
  • 23
  • use string methods for comparison. I mean use from.equalsIgnoreCase("maglist"); try this and let me know whether it works or not – nilkash Aug 20 '13 at 10:49

9 Answers9

1

In order to compare two strings you should do string1.equals(string2);
string1==string2 actually compares the references.

Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
0

To compare strings you should use 'equals':

if(from.equals("maglist")) {
// TO DO
}
else {
//To DO
}
Cole Tobin
  • 9,206
  • 15
  • 49
  • 74
Abhishek Birdawade
  • 301
  • 1
  • 2
  • 14
0

You should use the isequal method instead of "=="

from .equalsIgnoreCase("maglist")

It will work

JNI_OnLoad
  • 5,472
  • 4
  • 35
  • 60
0

Don't use == to compare strings, use equals instead, for example:

if(from.equals("maglist")){
    ...
}

More about compare strings

Community
  • 1
  • 1
Leszek
  • 6,568
  • 3
  • 42
  • 53
0

You need to compare Strings using equals, use if (from.equals("maglist"))

Jan Gressmann
  • 5,481
  • 4
  • 32
  • 26
0

Intead of == try from.equals("maglist")

Srikanth Roopa
  • 1,782
  • 2
  • 13
  • 19
0

change your condition with below one.

if(from.equals("maglist")){
        Log.d("refresh", "From = " + from);
    } else {
        Log.d("refresh", "dident work" + from);
    }

P.S:- You can not compare String values with equal(==) symbols, As they will return false always other then if both string objects are same.

AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

Right Way to compare The String in java

if(from.equals("maglist"))
   {
       ...
   }
Naveen Tamrakar
  • 3,349
  • 1
  • 19
  • 28
0

Try if(from.equals("maglist")). If that fails,

try if(from.contains("maglist"))

print x div 0
  • 1,474
  • 1
  • 15
  • 33