我试图让 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();
}
}
请帮我找出我的错误。