takayoshi
takayoshi

Reputation: 2799

pass data to activity using putExtra

I have a little problem I pass "0" to activity using putExtra and I get some strange result in log:

0 false

But it must be 0 true

What's matter

public class ItemEditCreate extends Activity{
String name;
EditText editText;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_create);

    if (savedInstanceState == null){
        Bundle extras = getIntent().getExtras();
        if (extras == null){
            name = null;
        }else{
            name = extras.getString("Item");
        }
    }else{
        name = (String) savedInstanceState.getSerializable("Item");
    }
    Log.i("Hello",name);
    Log.i("Hello",Boolean.toString((name == "0")));

    if (name == "0"){
       setTitle("Добавление элемента");
    }else{
        setTitle("Редактирование элемента");
        editText = (EditText) findViewById(R.id.edited_name);
        editText.setText(name);
    }
}
}

Upvotes: 0

Views: 265

Answers (5)

user370305
user370305

Reputation: 109237

You are doing wrong comparison, I think you have to use,

 Log.i("Hello",Boolean.toString((name.equals("0")));

EDIT::

Comparing Object references with the == and != Operators

The two operators that can be used with object references are comparing for equality (==) and inequality (!=). These operators compare two values to see if they refer to the same object. Although this comparison is very fast, it is often not what you want.

Usually you want to know if the objects have the same value, and not whether two objects are a reference to the same object. For example,

if (name == "Mickey Mouse")   // Legal, but ALMOST SURELY WRONG

This is true only if name is a reference to the same object that "Mickey Mouse" refers to. This will be false if the String in name was read from input or computed (by putting strings together or taking the substring), even though name really does have exactly those characters in it.

Many classes (eg, String) define the equals() method to compare the values of objects.

Comparing Object values with the equals() Method

Use the equals() method to compare object values. The equals() method returns a boolean value. The previous example can be fixed by writing:

if (name.equals("Mickey Mouse"))  // Compares values, not references.

Because the equals() method makes a == test first, it can be fairly fast when the objects are identical. It only compares the values if the two references are not identical.

Upvotes: 1

Sunil Kumar Sahoo
Sunil Kumar Sahoo

Reputation: 53657

Problem is in condition name == "0". == operator doesnt compare values of two strings. To compare value of two strings use equals() or equalIgnoreCase() methods

Use name.equals("0") or name.equalsIgnoreCase("0")

Upvotes: 1

Cata
Cata

Reputation: 11211

You have to use equals() method to compare two strings.. use

Boolean.toString((name.equals("0")))

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

You have implemented wrong string comparison i think:

if (name == "0")

instead you can implement:

name.equalsIgnoreCase("0");
 or 
name.equals("0");

Upvotes: 0

Caner
Caner

Reputation: 59198

To compare Strings for equality, don't use ==. The == operator checks to see if two objects are exactly the same object. Two strings may be different objects, but have the same value (have exactly the same characters in them). Use the .equals() method to compare Strings for equality.

Upvotes: 0

Related Questions