1. Are you ready for the Galaxy S20? Here is everything we know so far!

toggle MultiChoiceItems with a checkbox in a dialog

Discussion in 'Android Development' started by sariDon, Dec 4, 2013.

  1. sariDon

    sariDon Lurker
    Thread Starter

    I'm new to Android dev. I have a multichoice within a dialog populated from SQLite. Now I want a checkbox to toggle the check status of the multichoice items.

    The dialog opens fine. Multichoice items are populated correctly including the toggle checkbox.

    The problem is I cannot get the multichoice items toggle with the checkbox (ch1 in DelDialog.java) select.

    What I did:

    MainActivity.java (partial)

    [HIGH]public void delMessage(View view) {
    DialogFragment DDlg = new DelDialog();
    DDlg.show(getSupportFragmentManager(), "xyz");


    [HIGH]package com.example.myfirstdb;

    import java.util.ArrayList;
    import java.util.List;

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.DialogInterface;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.widget.CheckBox;
    import android.widget.CompoundButton;

    public class DelDialog extends DialogFragment {
    NoticeDialogListener mListener;

    public Dialog onCreateDialog(Bundle savedInstanceState) {
    final ArrayList mSelectedItems = new ArrayList();
    final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    ArrayList<String> al = new ArrayList<String>();
    DatabaseHandler db = new DatabaseHandler(getActivity());

    List<Contact> contacts = db.getAllContactsOrdered("name");

    for (Contact cn : contacts)
    al.add(cn.getName() + "\n" + cn.getPhoneNumber());

    final String[] ContactArray = (String[]) al.toArray(new String[0]);
    CheckBox ch1 = new CheckBox(builder.getContext());

    ch1.setText("Check All");

    ch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    int i = 0;
    if (isChecked) {
    while(i < ContactArray.length) {
    /* builder.setItemChecked(i, isChecked); */
    } else {
    // The toggle is disabled

    .setMultiChoiceItems(ContactArray, null,
    new DialogInterface.OnMultiChoiceClickListener() {
    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    if (isChecked) {
    // If the user checked the item, add it to the selected items
    } else if (mSelectedItems.contains(which)) {
    // Else, if the item is already in the array, remove it
    .setPositiveButton(R.string.del_button, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    .setNegativeButton(R.string.res_button, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

    return builder.create();

    public interface NoticeDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);

    public void onAttach(Activity activity) {
    try {
    mListener = (NoticeDialogListener) activity;
    } catch (ClassCastException e) {
    throw new ClassCastException(activity.toString()+ " must implement NoticeDialogListener");
    }[/HIGH]The line [HIGH]builder.setItemChecked(i, isChecked);[/HIGH] gives error. Thank you.

    1. Download the Forums for Android™ app!



Share This Page