I am trying use a switch case. But it says i cant pass a string 🙁
protected void onListItemClick(ListView l, View v, int position, long id) {
// super.onListItemClick(l, v, position, id);
String selection = l.getItemAtPosition(position).toString();
Log.i("Harsha", selection);
switch (selection) {
case "Compose":
break;
case "Inbox":
break;
case "Drafts":
break;
case "Sent":
break;
default:
break;
}
}
The error is
Cannot switch on a value of type String. Only convertible int values or enum constants are permitted
It is what it is! Java doesn’t allow you to pass String for a switch case statment, infact, the new Proposal was possibly rejected. ( read in a blog somewhere, i’m sorry, don’t have the source)
But it doesn’t mean you can’t do it by another method.
Alternatives
1) Use the position of the listitem, rather than the string.
2) Use Enum
3) Store it in a
Map<String,Integer>(or even an array), and use the Value in the SWitch CaseEDIT: Personally, i would add 4 constants and do it this way