我有一個活動,必須顯示AlertDialog與ListView供用戶選擇其中一個選項。當我將AlertDialog的代碼放入onCreate方法時,它可以工作。但是在其他地方,它不會。控制檯上沒有錯誤。AlertDialog不顯示時不創建
這裏是它被稱爲(在活動):
@Override
public void onStateChanged(IntegratorState state) {
switch (state.getState()) {
case AWAITING_MENU_OPTION:
IntegratorHelper.showOptionsMenu(state, SitefMenuActivity.this).show();
break;
default:
Toast.makeText(getApplicationContext(), state.getState().name(),
Toast.LENGTH_LONG).show();
}
}
這裏是我創建對話框(在其他類):
public static AlertDialog showOptionsMenu(IntegratorState state, Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(state.getGatewayMessageExtra());
String[] strings;
strings = state.getGatewayMessage().split(";");
final List<String> options = Arrays.asList(strings);
builder.setAdapter(new MenuSaleAdapter(context,
android.R.layout.simple_list_item_1, options),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == options.size()-1) {
// Do a thing
} else {
// Do other thing
}
}
});
return builder.create();
}
private static class MenuSaleAdapter extends ArrayAdapter<String> {
public MenuSaleAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView view = (TextView) super.getView(position, convertView,
parent);
view.setTextColor(Color.BLACK);
return view;
}
}
我試圖把所有的代碼在同一個活動中,但它仍然不起作用。該應用程序不凍結或任何東西,它只是不顯示對話框。有任何想法嗎?感謝
1.使用上下文菜單列表視圖+或2.使用彈出菜單。我強烈建議編號2. –
我不認爲這些菜單會做。主要是因爲他們的佈局我想。但我會試一試! –