我有一个Activity,必须显示带有ListView的AlertDialog,供用户选择其中一个选项。当我将AlertDialog的代码放在onCreate方法中时,它可以工作。但是在其他地方,它并没有。控制台上没有错误。
这就是它所在的地方(在活动中):
@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();
}
}
这是我创建Dialog的地方(在另一个类中):
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;
}
}
我试图将所有代码放在同一个Activity中,但它仍然无法正常工作。应用程序没有冻结或任何东西,它只是没有显示对话框。有任何想法吗?感谢
答案 0 :(得分:0)
尝试将第一部分更改为:
@Override
public void onStateChanged(IntegratorState state) {
switch (state.getState()) {
case AWAITING_MENU_OPTION:
IntegratorHelper.showOptionsMenu(state, SitefMenuActivity.this);
break;
default:
Toast.makeText(getApplicationContext(), state.getState().name(),
Toast.LENGTH_LONG).show();
}
}
和第二部分:
public static void showOptionsMenu(IntegratorState state, Activity activity) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(state.getGatewayMessageExtra());
String[] strings;
strings = state.getGatewayMessage().split(";");
final List<String> options = Arrays.asList(strings);
builder.setAdapter(new MenuSaleAdapter(activity,
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
}
}
});
builder.create().show();
}