jeudi 27 octobre 2016

how to get value of default checked checkbox in alert dialog android java

final List < CharSequence > checkedsurveylist = new ArrayList < CharSequence > ();
final JSONArray jsonArray = new JSONArray(response);
final boolean[] checkedItems = new boolean[jsonArray.length()];
for (int i = 0; i < jsonArray.length(); i++) {
  JSONObject obj = (JSONObject) jsonArray.get(i);
  String syncstatus = "";
  if (obj.get("syncstatus").toString().matches("1")) {
    syncstatus = "Not yet synchronized";
    checkedItems[i] = false;
  } else if (obj.get("syncstatus").toString().matches("2")) {
    syncstatus = "Synchronized";
    checkedItems[i] = true;
  }
  checkedsurveylist.add("(" + obj.get("sysid").toString() + ")" + obj.get("surveytitle").toString() + " - " + syncstatus);
}


System.out.println("checkedItems " + checkedItems);
final List < String > mSelectedItems = new ArrayList < String > ();
final List < Integer > mSelectedItemsindex = new ArrayList < Integer > ();
AlertDialog.Builder builder = new AlertDialog.Builder(Connect_server.this);
builder.setTitle("Survey List")
  .setMultiChoiceItems(checkedsurveylist.toArray(new CharSequence[checkedsurveylist.size()]), checkedItems,
    new DialogInterface.OnMultiChoiceClickListener() {


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

        checkedsurveylist.toArray()[which] = isChecked;
        // Get the current focused item
        String currentItem = checkedsurveylist.get(which).toString();

        // Notify the current action
        Toast.makeText(getApplicationContext(),
          currentItem + " " + isChecked, Toast.LENGTH_SHORT).show();
        if (isChecked) {

          mSelectedItems.add(checkedsurveylist.get(which).toString());
          mSelectedItemsindex.add(which);
        } else if (mSelectedItems.contains(which)) {
          mSelectedItemsindex.remove(Integer.valueOf(which));

          mSelectedItems.remove(Integer
            .valueOf(which));
        }
      }
    });
// Set the positive/yes button click listener
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                          
  @Override
  public void onClick(DialogInterface dialog, int which) {
    // Do something when click positive button
    System.out.println("asdasdasd " + mSelectedItemsindex.size());
    for (int i = 0; i < mSelectedItemsindex.size(); i++) {
      System.out.println("asdasdasd " + checkedsurveylist.get(i).toString());
    }

  }
});

This is my code for making a list with checkbox in alert dialog box. It can have multiple selection. my problem is I can get the value of default checked checkbox. I can only get the value if the checkbox is manually check by the user.

How to get the value of checkbox if it is by default checked.




Aucun commentaire:

Enregistrer un commentaire