public class MyActivity extends Activity {
SubMenu mSubmenu;
@Override
public boolean onCreateOptionsMenu
(Menu menu
) { mSubmenu = menu.addSubMenu("foo");
return true;
}
@Override
public boolean onPrepareOptionsMenu
(Menu menu
) { mSubmenu.clear();
int n
= (int) (Math.
random() * 10) + 1; for (int i = 0; i < n; i++) {
final double value
= Math.
random(); item.
setOnMenuItemClickListener(new MenuItem.
OnMenuItemClickListener() { public boolean onMenuItemClick
(MenuItem item
) { new AlertDialog.Builder(MyActivity.this)
.setMessage("value=" + value)
.setPositiveButton("OK", null).show();
return true;
}
});
}
return true;
}
}
cHVibGljIGNsYXNzIE15QWN0aXZpdHkgZXh0ZW5kcyBBY3Rpdml0eSB7CiAgICBTdWJNZW51IG1TdWJtZW51OwoKICAgIEBPdmVycmlkZQogICAgcHVibGljIGJvb2xlYW4gb25DcmVhdGVPcHRpb25zTWVudShNZW51IG1lbnUpIHsKCW1TdWJtZW51ID0gbWVudS5hZGRTdWJNZW51KCJmb28iKTsKCXJldHVybiB0cnVlOwogICAgfQoKICAgIEBPdmVycmlkZQogICAgcHVibGljIGJvb2xlYW4gb25QcmVwYXJlT3B0aW9uc01lbnUoTWVudSBtZW51KSB7CgltU3VibWVudS5jbGVhcigpOwoJaW50IG4gPSAoaW50KSAoTWF0aC5yYW5kb20oKSAqIDEwKSArIDE7Cglmb3IgKGludCBpID0gMDsgaSA8IG47IGkrKykgewoJICAgIGZpbmFsIGRvdWJsZSB2YWx1ZSA9IE1hdGgucmFuZG9tKCk7CgkgICAgTWVudUl0ZW0gaXRlbSA9IG1TdWJtZW51LmFkZChTdHJpbmcudmFsdWVPZih2YWx1ZSkpOwoJICAgIGl0ZW0uc2V0T25NZW51SXRlbUNsaWNrTGlzdGVuZXIobmV3IE1lbnVJdGVtLk9uTWVudUl0ZW1DbGlja0xpc3RlbmVyKCkgewoJCXB1YmxpYyBib29sZWFuIG9uTWVudUl0ZW1DbGljayhNZW51SXRlbSBpdGVtKSB7CgkJICAgIG5ldyBBbGVydERpYWxvZy5CdWlsZGVyKE15QWN0aXZpdHkudGhpcykKCQkJICAgIC5zZXRNZXNzYWdlKCJ2YWx1ZT0iICsgdmFsdWUpCgkJCSAgICAuc2V0UG9zaXRpdmVCdXR0b24oIk9LIiwgbnVsbCkuc2hvdygpOwoJCSAgICByZXR1cm4gdHJ1ZTsKCQl9CgkgICAgfSk7Cgl9CglyZXR1cm4gdHJ1ZTsKICAgIH0KfQ==