• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps toggle MultiChoiceItems with a checkbox in a dialog

sariDon

Lurker
Dec 4, 2013
1
0
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]DelDialog.java:

[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 {
@SuppressLint("NewApi")
NoticeDialogListener mListener;


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreate(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); */
i++;
}
} else {
// The toggle is disabled
}
}
});

builder
.setTitle(R.string.del_title)
.setCustomTitle(ch1)
.setMultiChoiceItems(ContactArray, 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.del_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogPositiveClick(DelDialog.this);
}
})
.setNegativeButton(R.string.res_button, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//LoginDialogFragment.this.getDialog().cancel();
}

});
return builder.create();
}

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

@Override
public void onAttach(Activity activity) {
super.onAttach(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.
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones