Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 775
Irfan Tri Atmojo Avatar asked May 02 '26 09:05

Irfan Tri Atmojo


2 Answers

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

like image 180
shyamal Avatar answered May 05 '26 00:05

shyamal


use

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

as this is more precise

like image 37
Its not blank Avatar answered May 04 '26 22:05

Its not blank