I am presenting user with a alert dialog which contains 2 items, I want to implement an OnClickListener for both items. I am able to set 1 item but when I try to use switch statement, I get this error :
Cannot switch on a value of type CharSequence[]. Only convertible int values or enum constants are permitted
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
final CharSequence[] items = {"Reviews", "More Info"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please Select an Option");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
switch (items){
case 1 :
//do something
case 2:
Intent intent = new Intent (MyActivity.this, WebViewActivity.class);
MyActivity.this.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
Using strings in a switch case for a menu?
Strings in switch statements were added in Java 7. For an example, take a look here. Since Android development isn’t currently based on Java 7 syntax, you’ll have to go the alternate route. And that means: if-else statements. They aren’t the prettiest, but they’ll get the job done.
so can try
http://www.botskool.com/geeks/how-create-dialog-box-android-part-2