jtate
jtate

Reputation: 2696

(Android) List in dialog with only one item with a checkbox

Ok, I've searched for this for a while now, but I can't find anything. My app displays a students school schedule in a list and when the user clicks a class in the list it displays a dialog that lists a few options (Edit, Delete, Set Alarm). Edit and Delete are easy, because they are clicked and something happens, but I need help with the "Set Alarm" option. I don't want it to be clickable, I just want a checkbox to the right of it that will toggle an alarm on or off. Here's the code for my dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle(courseName)
    .setItems(R.array.courseList, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            switch(which) {
            case LIST_EDIT:
                break;
            case LIST_DELETE:
                break;
            case LIST_ALARM:
                break;
            }
        }
    });

    AlertDialog alert = builder.create();
    alert.show();

Right now I have the list options in a string array in my resources xml file with the id courseList. LIST_EDIT, LIST_DELETE, and LIST_ALARM are final int's corresponding to their index in the list. I'm really not sure how to add a checkbox to the alarm list item, any help would be appreciated.

Upvotes: 4

Views: 381

Answers (1)

An-droid
An-droid

Reputation: 6485

If you are familiar with fragment you can use a FragmentDialog and then in the method onCreateDialog you create the dialog as you want.

LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.list, null);
_list = (ListView) view.findViewById(R.id.listview);
_adapter = new AdapterDialog(_context);
_list.setAdapter(_adapter);
//
AlertDialog.Builder builder = new AlertDialog.Builder(_context);
builder.setView(view);  builder.setTitle("Dialog").setPositiveButton(getActivity().getString(android.R.string.ok), this);

return builder.create();

Or something like that And then create the dialog with a singleton method or a constructor

_dialog = Dialog.newInstance(R.string.title, this);
_dialog.setCancelable(true);
_dialog.show(getSupportFragmentManager(), null);

With your aproach you can't add something more than the "buttons" if you want to have a custom row in your list you must use an adapter

hope it helps

Upvotes: 1

Related Questions