- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试图让 AlertDialog 出现,让用户从列表中选择几个项目。这应该非常简单,我遵循了本指南: http://developer.android.com/guide/topics/ui/dialogs.html但是我的元素没有出现!我只是得到一个包含消息和按钮的对话框,没有列表项...
这是我的代码:
public class PlayerPickerDialogFragment extends DialogFragment {
//OK - so I tried making a CharSequence array, just to be sure where the error was...
final CharSequence[] names = {"cgu", "carl"};
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final ArrayList mSelectedItems = new ArrayList(); // Where we track the selected items
System.out.println(PersistentData.getInstance().getPlayerNames().toString());
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.pick_players)
//.setMultiChoiceItems(PersistentData.getInstance().getPlayerNames(), null, new DialogInterface.OnMultiChoiceClickListener() {
.setMultiChoiceItems(names, null, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
mSelectedItems.add(which);
} else if (mSelectedItems.contains(which)) {
// Else, if the item is already in the array, remove it
mSelectedItems.remove(Integer.valueOf(which));
}
}
})
.setPositiveButton(R.string.play,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ArrayList<Player> players = new ArrayList<Player>(
PersistentData.getInstance().getPlayers());
GameState newGame = new GameState(players);
PersistentData.getInstance().setGameState(newGame);
Intent intent = new Intent(getActivity(),
GameActivity.class);
startActivity(intent);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
请帮我找出错误。
最佳答案
您可以使用下面的简单代码代替 Fragment。它在对话框中显示列表。
public class MainActivity extends Activity {
private boolean[] gradeOptions = new boolean[12];
String item[] = { "Grade1", "Grade2", "Grade3", "Grade4", "Grade5",
"Grade6", "Grade7", "Grade 8", "Grade9", "Grade10", "Grade11",
"Grade12" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Grade");
builder.setMultiChoiceItems(item, gradeOptions,
new DialogGradeSelectionClickHandler());
builder.setCancelable(false)
.setNegativeButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog diag = builder.create();
diag.show();
}
等级选择ClickHandler
class DialogGradeSelectionClickHandler implements
DialogInterface.OnMultiChoiceClickListener {
public void onClick(DialogInterface dialog, int clicked,
boolean selected) {
gradeOptions[clicked] = selected;
}
}
关于android - 列表项不出现在 AlertDialog 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23291866/
我正在尝试在 AlertDialog 中创建一个 AlertDialog,但是当我运行代码时,没有出现第二个 AlertDialog 这是我的代码,我想让它像如果用户在第一个 AlertDialog
相关问题是here 制作android应用,我觉得我的代码不酷。 因为,每当需要对话框时我都会创建新的 AlderDialog.Builder 以防止出现此错误 “指定的子项已有父项。您必须先对子项的
我正在使用以下代码来创建警报对话框。 AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(thi
我正在尝试在警报对话框中添加一个警报对话框。但是看不到第二个警报对话框。请帮助我这是我显示的代码 AlertDialog alertDialog = new AlertDialog.Builder(m
为什么要使用 AlertDialog.Builder 类而不是 AlertDialog 直接可用的方法,例如,为什么使用 AlertDialog.Builder.setCancellable 而不是
假设您有一个带有两个按钮 A 和 B 的 AlertDialog。我发现在某些设备和某些版本的 android 上,如果您触摸对话框周围屏幕的任何区域,AlertDialog 消失。在其他设备上,您被
我目前正在使用需要使用大量 AlertDialogs 的应用程序。我目前在这里编写了一个基本的代码: protected void StopButton () { AlertDialog.Bu
我正在开发一个 Android 应用程序,并且我有一个 AlertDialog 子类。我想在对话框标题区域的右侧放置 2 个 ImageButtons(类似于 Activity 中的 ActionBa
我一直在尝试在 AlertDialog 中制作一个按钮Flutter 中的盒子。但我找不到拉伸(stretch)按钮容器的方法。请检查我的代码并查看下面的示例图片。 AlertDialog(
我正在尝试找出创建对话框的最佳方式。我可以创建自己的 Dialog 类(对我来说,它更干净、更有条理),或者我可以使用 AlertDialog.Builder(可以内联完成,而且看起来很时髦)……两者
我使用 AlertDialog.builder 创建了一个对话框,其中显示了可以检查的多选项目列表。 我设置了初始的项目名称集及其检查状态: builder.setMultiChoiceItems(
应用拦截短信并显示消息的对话框。 但是我无法在我的 Test 类中解决我的 Dialog 错误。我做错了什么? (我还包含了我的其他 2 个文件)。 Eclipse 中显示错误:AlertDialog
我的 flutter 应用程序中出现了一个 Flutter AlertDiaog。使用 Flutter Driver,我无法点击 Flutter AlertDialog 或 AlertDialog 上
我是 Jetpack compose 的初学者。现在在我的应用程序屏幕中,AlertDialog 用于向用户显示一些信息。 根据文档,当用户在对话框外或后退按钮上单击时,将调用 onDismissRe
我有一个 AlertDialog 的自定义子类,它应该显示范围内所有可用 Wifi 网络的列表。 我通过创建该对话框的实例并调用 show() 来显示此对话框,并且我没有使用 AlertDialog.
我有一个 AlertDialog 的子类,它应该显示范围内所有可用 Wifi 网络的列表。 我希望对话框本身负责启动 Wifi 扫描并接收结果。 出于这个原因,我不能使用 AlertDialog.Bu
标题几乎说明了一切。我需要关于如何做到这一点的建议。我可能只是将外部适配器添加为 AlertDialog 上的 View,以使其不那么复杂,但我仍然不知道如何与 交互来自内部适配器的 AlertDia
我想自定义 V7 AlertDialog 的 TITLE 颜色。 SetCustomTitle() 似乎无法与 android.support.v7.app.AlertDialog 一起使用。我可以看
创建 AlertDialog 然后显示和显示 AlertDialog.Builder 本身之间的主要区别是什么? 例如。我可以有一个像这样的 AlertDialog.Builder: AlertDi
关于我们什么时候应该使用 android.app.AlertDialog,或者我们什么时候应该使用 android.support.v7.app.AlertDialog,是否有任何指南? 因为,如果我
我是一名优秀的程序员,十分优秀!