0

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();
Akhil Latta
  • 1,603
  • 1
  • 22
  • 37

2 Answers2

0

Android runs JRE 6. If i'm not mistaken. Performing a switch(CharSequence[]) and switch(String) wasn't allowed until JRE 7 which came out this year. You'll have to do a series of if else statements to go around this.

Ryan Gray
  • 824
  • 8
  • 17
0

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

 public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub

           switch (which){

            case 0 :
                    String str = items[which];
                //do something

            case 1:

                Intent intent = new Intent (MyActivity.this, WebViewActivity.class);
                MyActivity.this.startActivity(intent);


            }
        });

http://www.botskool.com/geeks/how-create-dialog-box-android-part-2

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36