Reputation: 408
I'm a complete newbie to Android, and this may actually be more of a Java question...
I'm trying to set up an if statement based on the item clicked in a ListView. In my onItemClick method I have the following:
Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
if(((TextView) view).getText() == "Page 1"){
Toast.makeText(getApplicationContext(), "Page 1 clicked", Toast.LENGTH_SHORT).show();
}
The string populating my ListView has "Page 1"; I see the first Toast saying "Page 1", but I never get the "Page 1 clicked" Toast, so obviously something about my if statement is wonky. Suggestions?
Thanks!
Bill
Upvotes: 1
Views: 1974
Reputation: 52002
When comparing Strings in Java, you should use equals()
:
if (((TextView) view).getText().equals("Page 1")) {
...
}
Upvotes: 1