0

I need to uncheck checked checkbox programmaticaly, but the check list is in dialog. Does this changes solution? What I have so far:

CharSequence[] items = itemList.toArray(new CharSequence[itemList.size()]);

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select categories");

builder.setMultiChoiceItems(items, null,
new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int which, boolean isChecked) {
  if (isChecked){
    if(chosenCat.size() < 10){
      chosenCat.add(items[which].toString());
    }else{
      Toast.makeText(getApplicationContext(), "Max 10 categories to compare.",   Toast.LENGTH_SHORT).show();
      //uncheck
      items.isChecked = false; // --> wrong
      chosenCat.remove(items[which].toString());
    }
  }else{
    chosenCat.remove(items[which].toString());
  }
}
});
Arnes
  • 403
  • 1
  • 5
  • 20
  • Actually what you want? you want to show list with alert box ? you can use your list view as alert dialog using theme in manifest .. – itsrajesh4uguys May 23 '13 at 08:55
  • View the previous post fot [setItmChecked][1] [1]: http://stackoverflow.com/questions/5067641/how-uncheck-items-in-alertdialog-setmultichoiceitems – Daniele75 Jan 25 '14 at 16:09
  • See previous post [SetItemCheck][1] [1]: http://stackoverflow.com/questions/5067641/how-uncheck-items-in-alertdialog-setmultichoiceitems – Daniele75 Jan 25 '14 at 16:18

2 Answers2

3

I've missed this line

((AlertDialog) dialog).getListView().setItemChecked(which, false);

after

checkboxStatusArray[1] = false;
Arnes
  • 403
  • 1
  • 5
  • 20
0

Pass boolean[] into builder.setMultiChoiceItems which indicates checkbox status(checked/unchecked) for the checkboxes added respectively.Try this sample code

boolean[] checkboxStatusArray = {true,false};

CharSequence[] items = itemList.toArray(new CharSequence[itemList.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select categories");

builder.setMultiChoiceItems(items, checkboxStatusArray,
new DialogInterface.OnMultiChoiceClickListener() {

public void onClick(DialogInterface dialog, int which, boolean isChecked) {


                      checkboxStatusArray[1] = true;//This line will enable second check box



     }
 });

builder.show();
swetha kini
  • 564
  • 1
  • 6
  • 23
  • final boolean[] checkboxStatusArray = {true,false}; CharSequence[] items = {"one","two","three"}; AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Select categories"); builder.setMultiChoiceItems(items, checkboxStatusArray, new DialogInterface.OnMultiChoiceClickListener() { public void onClick(DialogInterface dialog, int which, boolean isChecked) { checkboxStatusArray[1] = true;//This line will enable second check box } }); builder.show(); I can't get it to work. :( – Arnes May 23 '13 at 09:08
  • you are adding 3 checkbox items, so add 3 boolean values in checkboxStatusArray – swetha kini May 23 '13 at 09:12
  • ya..it is checked because of the line `checkboxStatusArray[1] = true;` What you wanted? you can modify this according to you. – swetha kini May 23 '13 at 09:22
  • In the debuger after "checkboxStatusArray[0] = false;" line is executed checkboxStatusArray[0] stays on true; -- Do I need refresh or something? – Arnes May 23 '13 at 10:50
  • This code works only when you click your checkbox(any).Supppose you click your first checkbox and if your second checkbox is unchecked then this code will check your second checkbox. – swetha kini May 23 '13 at 11:00
  • Missing: ((AlertDialog) dialog).getListView().setItemChecked(which, false); – Arnes May 23 '13 at 11:11