无法将复选框的状态作为ListView项的子元素

时间:2012-04-28 12:53:27

标签: android checkbox android-listview

在我的代码中,我使用的结构如下图所示:

enter image description here

检查CheckBox后,在send Button点击后,我正在阅读CheckBoxes的状态,就像下面写的代码一样,但它显示我未经检查。 CheckboxName.isChecked()值为false,但检查了我的CheckBox

  try {
                int nCount = NotebookList.getCount();
                LinearLayout v;
                View vv;
                CheckBox Available, Display, Demo;
                v = (LinearLayout) findViewById(R.id.LinearNotebookList);
                TextView _id, ID, Name;
                for (int i = 0; i < nCount; i++) {
                    v = (LinearLayout) NotebookList.getAdapter().getView(i, null,
                            null);
                    _id = (TextView) v.findViewById(R.id.txt_Notebook_ID);

                    Available = (CheckBox) v
                            .findViewById(R.id.chkboxNotebookAvailable);
                    Display = (CheckBox) v.findViewById(R.id.chkboxNotebookDisplay);
                    Demo = (CheckBox) v.findViewById(R.id.chkboxNotebookDemo);
                    str_id = (String) _id.getText();

                    if (Available.isChecked()) {
                        strAvailable = "YES";
                    } else {
                        strAvailable = "NO";
                    }
                    if (Display.isChecked()) {
                        strDisplay = "YES";
                    } else {
                        strDisplay = "NO";
                    }
                    if (Demo.isChecked()) {
                        strDemo = "YES";
                    } else {
                        strDemo = "NO";
                    }

                    rowID += db.insertAudit(Integer.parseInt(UserId),
                            Integer.parseInt(CityId), Integer.parseInt(OutletId),
                            0, dateTime, "002", Integer.parseInt(str_id),
                            strAvailable, strDisplay, strDemo, null, null, null,
                            null, null, null, null, null, null, null, Instance_Id)
                            + ",";

                }
            } catch (Exception ex) {
                Toast.makeText(getApplicationContext(), ex.getMessage(),
                        Toast.LENGTH_SHORT).show();
            }

我找到了一些可以提供帮助的内容

如果是的话,我可以用以下方式做点什么吗

LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.desktop_list, parent, false);

1 个答案:

答案 0 :(得分:1)

要使复选框在listView上正常工作,您需要使用BaseAdapter类