I am using AlertDialog with setMultiChoiceItems to let the user select multiple items which is working fine. The problem is, next time the AlertDialog appears, it still has the items checked. I tried unchecking them by overriding onPrepareDialog but it is not working. This is my code:
@Override
protected Dialog onCreateDialog( int id )
{
String[] PROJECTION=new String[] { Contacts._ID,
Contacts.DISPLAY_NAME,
Phone.NUMBER};
String number = null;
String[] ARGS={String.valueOf(Phone.TYPE_MOBILE)};
c=managedQuery(Phone.CONTENT_URI,
PROJECTION, Phone.TYPE+"=?",
ARGS, Contacts.DISPLAY_NAME);
while (c.moveToNext()) {
number = c.getString(1);
names.add(number);
numbers.add(c.getString(2));
}
CharSequence[] cs = names.toArray(new CharSequence[names.size()]);
selection = new boolean[names.size()];
return new AlertDialog.Builder(this)
.setTitle("Pick Contacts")
.setMultiChoiceItems(cs,
selection, new DialogInterface.OnMultiChoiceClickListener(){
public void onClick(DialogInterface dialog, int whichButton,
boolean isChecked) {
if(isChecked){
names1.add(names.get(whichButton));
numbers1.add(numbers.get(whichButton));
isChecked = false;
}else{
names1.remove(names.get(whichButton));
numbers1.remove(numbers.get(whichButton));
}
}
})
.setPositiveButton( "OK", new DialogButtonClickHandler() )
.setNegativeButton( "Cancel", new DialogButtonClickHandler1() )
.create();
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
final AlertDialog alert = (AlertDialog)dialog;
final ListView list = alert.getListView();
for(int i = 0 ; i < list.getCount(); i++){
list.setItemChecked(i, false);
}
}
I tried checking all the items by using list.setItemsChecked(i,true) and its working but unchecking doesn't work. Any ideas?
I resolved this issue, and the code, which is presented below, already is applied in my app "Email Pictures Automatically":
https://play.google.com/store/apps/details?id=com.alexpap.EmailPicturesFree
This app is sending automatically every picture you make to a list of recipient emails. The functionality below is used to select a list of recipients, which will receive the pictures you make. Pictures are sending instantly. Whole code is in EmailParametersActivity class. Works with minSdkVersion="5".
//Some definitions in onCreate () method
//end of onCreate() method
And finally here is the function which gets conacts with names and emails
To clear the checkboxes you simply have to go through your boolean array "selection" and set all entries to false.