I'm trying to make an AlertDialog appear, that let's the user pick several items from a list. That should be pretty straightforward, and i followed this guide: http://developer.android.com/guide/topics/ui/dialogs.html But my items do not appear! I just get a dialog with the message and the buttons, no listitems...

This is my code:

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();
}

}

Please help me find my error.

有帮助吗?

解决方案

Instead of using Fragment you can use simple below code. It is showing list on dialog.

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();

}

grade selectionClickHandler

class DialogGradeSelectionClickHandler implements
        DialogInterface.OnMultiChoiceClickListener {
    public void onClick(DialogInterface dialog, int clicked,
            boolean selected) {
        gradeOptions[clicked] = selected;
    }
}

其他提示

Dialog fragment will work like normal fragment . so use custom XML file create what ever you

want . Like following

public class TestDialogFragment extends DialogFragment implements
OnClickListener {

    protected String LOG = "FdshistoryDialogFragment";

    // ====== widgets ========
    Button save_button, close_button;
    ListView _listView;
    private static List<String> mList ;


    /**
     * Create a new instance of MyDialogFragment, providing "ROS details" as an
     * argument.
     */
    public static TestDialogFragment newInstance(String title,
            List<String> mMainlist) {

        TestDialogFragment f = new TestDialogFragment();
        mList = new ArrayList<String>();

        mList = mMainlist;
        // Supply input as an argument.
        Bundle args = new Bundle();
        args.putString("title", title);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        int style = DialogFragment.STYLE_NORMAL, theme = android.R.style.Theme_Holo_Light_Dialog;
        setStyle(style, theme);



    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        String mTitle = getArguments().getString("title");
        getDialog().setTitle(mTitle);

        View view = inflater.inflate(R.layout.finaldiagno_historylayout,container);

        _listView = (ListView) view.findViewById(R.id._listView);

        save_button = (Button) view.findViewById(R.id.save_button);
        close_button = (Button) view.findViewById(R.id.close_button);


        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        save_button.setOnClickListener(this);
        close_button.setOnClickListener(this);

        TestAdapter alertAdapter = new TestAdapter(getActivity(), mList);
        _listView.setAdapter(alertAdapter);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        case R.id.save_button:



            break;
        case R.id.close_button:

            getDialog().dismiss();

            break;
        }
    }   
}

Try this it will work

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top