How to compare a String from Intent.getStringExtra()?

android, android-intent, java

Solution

When comparing two strings to check if they are equal in java you should use equals().

In your case you should change your comparison line to

 if(path.equals("car")) 

In java the equals() compares the actual contents of your strings while == will only compare to see if the two references are equal

Problem

Can I compare the data from `getStringExtra()` with a string? Sample code: ``` Intent intent = getIntent(); String path = intent.getStringExtra("category"); if(path == "car"){ setListAdapter(new ArrayAdapter<String>(this, R.layout.subcategory, car)); } else { setListAdapter(new ArrayAdapter<String>(this, R.layout.subcategory, bike)); } ``` Why is the script is not running?

Original source