【问题标题】:how to get value of default checked checkbox in alert dialog android java如何在警报对话框android java中获取默认选中复选框的值
【发布时间】:2017-03-09 20:23:20
【问题描述】:
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);

我使用了这个,但是点击 OK 按钮,但我在位置上得到 -1,在哪个位置上得到 -1

【问题讨论】:

  • 在 CheckBox 的实例上调用 isChecked()
  • 你能告诉@Blackbelt在哪里以​​及如何
  • 为什么不在for 循环中将预先检查的项目添加到您的ArrayLists 中?如果您不想这样做,AlertDialog#getListView().getCheckedItemPositions() 将返回带有选中位置的SparseBooleanArray
  • 再看一遍。 getCheckedItemPositions(),复数。
  • @MikeM。它起作用了我用这个ListView lv = ((AlertDialog) dialog).getListView(); SparseBooleanArray checkedItems = lv.getCheckedItemPositions();你能把它作为答案发布,这样我就可以关闭这个OP

标签: java android checkbox android-alertdialog


【解决方案1】:

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

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

将它们放在一起,您可以得到所有选定的项目,包括在展示时检查的项目。

【讨论】:

  • 迈克还有一个问题是有一种方法可以在每个列表项上添加属性数据。例如,未显示在列表中但可以在选择选中项目时访问的数据。例如添加.setTag()?
  • 嗯,不直接。我要做的可能是创建一个简单的自定义类implements CharSequence,将您的setTag()/getTag() 方法和字段添加到其中,然后将该类用于checkedsurveylist。只要toString() 方法返回您现在添加到列表中的串联String,就用户界面而言,这将是一个透明的更改。
  • 再问一个问题,我想禁用预先选中的复选框,我在 lv.getChildAt(checkedItems.keyAt(i)).setEnabled(false); 内部使用了 if (checkedItems.get(i)) { 知道如何禁用预先选中的复选框吗?
  • 我怀疑你可以通过AlertDialog 轻松获得你想要的行为。您可以在AlertDialog 上设置自定义ListAdapter,但我很确定默认行为是在单击项目后关闭它,因此多重检查可能不起作用。如果您只是自定义Dialog,可能会更容易。如果我想办法简单地使用AlertDialog,我会告诉你的,但我认为没有。
猜你喜欢
  • 2023-03-11
  • 2017-09-14
  • 2020-05-16
  • 1970-01-01
  • 2013-04-10
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 2013-06-26
相关资源
最近更新 更多