menu event handling

2019-08-23 05:38发布

问题:

I want to set a list of choice after selected the language choice in menu list.

The list works in the normal button case (onClick) but menu.

Anything I should notice?

sorry for my english

switch (item.getItemId()) {
        case R.id.changeCity:
            showInputDialog();
            return true;
        case R.id.Language:
            List<String> language = new ArrayList<String>();
            language.add(getString(R.string.english));
            language.add(getString(R.string.chinese));
            new AlertDialog.Builder(WeatherClass.this)
                    .setSingleChoiceItems(language.toArray(new String[language.size()]), 0,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                }
                            })
                    .show();
              return true;
        default:
            return super.onOptionsItemSelected(item);
    }

what I want



Show the list like

回答1:

First show dialog of language selection and according to position or text show another dialog with content.

 case R.id.Language:
   final CharSequence[] items = { "English", "Chinese"};

   AlertDialog.Builder builder = new AlertDialog.Builder(WeatherClass.this);
   builder.setTitle("Selection");
   builder.setItems(items, new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int position ) {

           List<String> language = new ArrayList<String>();

           if (position == 0) {           
                // language = "english data"
           }
           if (position == 1) {       
                // language = "chinese data"        
           }
           new AlertDialog.Builder(WeatherClass.this).setSingleChoiceItems(language.toArray(new String[language.size()]), 0, new DialogInterface.OnClickListener() {
                  @Override
                  public void onClick(DialogInterface dialog, int which) {
                           // second dialog data
                  }
            })
          .show();
       }
   }).show();
  return true;