How to compare string with Intent.getStringExtra ()?

Is it possible to compare data from getStringExtra()with a string? Code example:

  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 does the script not work?

+3
source share
3 answers

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

In your case, you should change the comparison line to

 if(path.equals("car")) 

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

+4
source

using

path.equals("car") "or"
path.contains("car") "or"
path.equalsIgnoreCase("car") "or"

since it is more accurate

+2
source

== contrast is the memory address equal to contrast is the attribute value.

0
source

All Articles