0

I have been stuck on this for 2 days now, I don't see why the below code is not working, the problem seems to be the "if" block on the onItemClick, please I'lld really appreciate if anyone can help me out with this. Thanks.

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String name = parent.getItemAtPosition(position).toString(); Toast.makeText(SearchActivity.this, name,Toast.LENGTH_LONG).show(); if (name=="Sweet Tooth"){ Intent intent = new Intent(SearchActivity.this,SwitActivity.class); startActivity(intent); } } }); 

3 Answers 3

1

Try this :

String name = listview.getItemAtPosition(position).toString(); 

And

if (name.equals("Sweet Tooth")){ Intent intent = new Intent(SearchActivity.this,SwitActivity.class); startActivity(intent); } 
Sign up to request clarification or add additional context in comments.

Comments

0

in java String == String will always return false.

use this instead.

String s = "Sweet Tooth"; if (s.equals("Sweet Tooth"){ //code } 

Comments

0

In Java, you compare two Strings by using equals function.

String name = parent.getItemAtPosition(position).toString(); if (name.equals("Sweet Tooth")){ startActivity(new Intent(SearchActivity.this,SwitActivity.class)); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.