android - 列表项不会出现在 AlertDialog 中

标签 android android-alertdialog multichoiceitems

我试图显示一个 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/

相关文章:

Android fragment : The fragment is not getting displayed. 没有警告,没有错误

c# - 即使应用程序处于后台,Time.unscaledDeltaTime 仍然在计数

java - Android执行一段代码时出现内存泄漏问题

android - 在 AlertDialog 中将文本设置为粗体

Android Alert对话框WindowManager$BadTokenException异常问题

android - 如何突出显示或检查选定的列表项

android - DialogFragment 按钮推出屏幕 API 24 及更高版本

php - Android 从 PHP 传递和接收参数

Java OutOfMemory 异常无法在 Lollipop 之前的设备上启动应用程序

java - 如何在警报对话框中保存多选状态?