0

I am working on an android app. I have a string in one of my first activity. And I use intent.putExtra() to pass that string to second activity which I started in first activity. Here are the related parts of activity codes:

First Activity

final ListView lv = (ListView) findViewById(R.id.listView1);
        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
        String selectedFromList = (lv.getItemAtPosition(arg2).toString());

        Intent content_umre_Intent = new Intent("com.uygulama.hacc.ContentActivity");
        content_umre_Intent.putExtra("key", selectedFromList);
        startActivity(content_umre_Intent);
    }
 });

Second Activity

Context context = getApplicationContext();                      
int duration = Toast.LENGTH_SHORT;

Toast toast = Toast.makeText(context, text, duration);
String text = getIntent().getStringExtra("key").toString();

if(text=="abc"){
    toast.show();
}

There is something curios here. I put toast.show() outside of if statement, and I saw that it is equal to "abc".

However even the variable text is exactly equal to "abc" it doesnt go enter the if statement.

Do you have an idea why this happens? I couldnt find any reason. I really need help right now.

Any help would be appreciated.

user2870
  • 477
  • 7
  • 18

3 Answers3

2

Nothing too curious...change

if(text=="abc"){

to

if("abc".equals(text)){

When comparing Strings, == will compare if they reference the same object. You need .equals() to compare if they have the same value.

You can do it as

if(text.equals("abc")){

but the first way I posted will protect against a NPE.

Off topic

Why do you have toString() on a String value?

String text = getIntent().getStringExtra("key").toString();

Also, if you are inside of the Activity and not inside of an inner class or listener then you can use this for your Context instead of creating a variable. It probably won't hurt anything here but depending on what you are using the Context for it could cause problems since you are using Application Context instead of Activity Context...just be aware.

codeMagic
  • 44,549
  • 13
  • 77
  • 93
1

Try

if(text.equals("abc")){
    toast.show();
}
Andromeda
  • 1,370
  • 2
  • 10
  • 15
0

== tests for reference equality.

.equals() tests for value equality

Its a java string comparison it works with equals

if(text.equals("abc")){
    toast.show();
}

But i would recommend

if("abc".equals(text)){
    toast.show();
}

because if text is null first will throw Exception but second won't it will be automatically handle with .equals(object)

and also you can do this if you don't bother about the case

"abc".equalsIgnoreCase(text)
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
  • This is true if the `Class` under question has overridden its `equals()` to test for values specific for that `Class`. It is up to the programmer to decide what `equals()` compares. By default `equals()` and `==` are equivalent; they both check for memory reference equality. – Emmanuel Sep 30 '13 at 15:03