如何在警告对话框android java中获取默认选中复选框的值

时间:2016-10-27 08:28:06

标签: java android checkbox android-alertdialog

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());
    }

  }
});

这是我在警告对话框中创建带复选框的列表的代码。它可以有多种选择。我的问题是我可以获得默认选中复选框的值。如果用户手动检查复选框,我只能获取值。

如果默认选中复选框,如何获取复选框的值。

Update

ListView lv = ((AlertDialog) dialog).getListView();
   System.out.println("asd" + "pos = " + lv.getCheckedItemPosition());
   System.out.println("asd" + "which = " + which);

我使用了这个但是点击了确定按钮,但我得到了-1的位置和-1的

1 个答案:

答案 0 :(得分:1)

在包含列表项的AlertDialog中,您可以使用ListView方法获取默认getListView()

在具有多项选择模式的ListView中,getCheckedItemPositions()将返回带有选中项目的SparseBooleanArray

将它们组合在一起,您就可以获得所有选定的项目,包括在显示时检查的项目。

相关问题