在自定义列表视图中选中我的复选框时随机勾选/检查

时间:2015-06-04 05:51:45

标签: android listview

在自定义列表视图中选中我的复选框,或者甚至向上或向下滚动检查已经完成了我甚至没有点击或触摸的复选框。在这里,我附加了getView方法,

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    if (transList != null) {
        final int pos = position;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.export_item_list, null);
            checkBox = (CheckBox) convertView.findViewById(R.id.export_chk);
            txtOrdNo = (TextView) convertView.findViewById(R.id.txt_exportOrdNo);
            txtOrdDate = (TextView) convertView.findViewById(R.id.txt_exportDate);
            txtOrdStatus = (TextView) convertView.findViewById(R.id.txt_exportStatus);
            convertView.setTag(new OrdersViewHolder(checkBox, txtOrdNo,
                        txtOrdDate, txtOrdStatus));
            // checkbox click function
            checkBox.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox cb = (CheckBox) v;
                    if (cb.isChecked()) {
                        cb.setChecked(true);
                        // save item in preference
                        String itemName = transList.get(pos).getHhTran_referenceNumber();
                        supporter.saveOrderInPreference(itemName, 1);

                        //to check and uncheck selectAll checkbox
                        if(!selectAll.isChecked()) {

                            boolean selectCheck = true;
                            int c=exportItems.getChildCount();
                            for(int i=0; i<exportItems.getChildCount();i++) {
                                LinearLayout listViewLayout = (LinearLayout) exportItems.getChildAt(i);
                                CheckBox chkBox = (CheckBox) listViewLayout.findViewById(R.id.export_chk);
                                System.out.println("i="+i);
                                if(!chkBox.isChecked()) {
                                    selectCheck = false;
                                    System.out.println("i inside="+i);
                                    break;
                                }
                            }

                                if(selectCheck){
                                    selectAll.setChecked(true);
                                    supporter.setSelectAllCheckedForOrder();
                                }

                            }
                        } else {
                            cb.setChecked(false);
                            supporter.removeOrderFromPreference(transList.get(pos).getHhTran_referenceNumber());

                            //to check and uncheck selectAll checkbox
                            if(selectAll.isChecked()) {
                                selectAll.setChecked(false);
                                supporter.setSelectAllNotCheckedForOrder();
                            }
                        }
                    }
                });

                boolean isExist = isOrderExist(transList.get(pos).getHhTran_referenceNumber());

                if (isExist) {
                    // set qty
                    // int qty =
                    // getMyItem(reciptList.get(pos).getHhReceipt_docnumber());
                    checkBox.setChecked(true);
                } else {
                    checkBox.setChecked(false);
                }

            } else {
                OrdersViewHolder viewHolder = (OrdersViewHolder) convertView
                        .getTag();
                checkBox = viewHolder.getCheckBox();
                txtOrdNo = viewHolder.getTxtOrdNo();
                txtOrdDate = viewHolder.getTxtOrdDate();
                txtOrdStatus = viewHolder.getTxtOrdStatus();
            }

            int day = transList.get(position).getHhTran_transDay();
            int month = transList.get(position).getHhTran_transMonth();
            int year = transList.get(position).getHhTran_transYear();
            String strDate = supporter.getStringDate(year, month, day);

            int stusVal = transList.get(position).getHhTran_status();
            System.out.println("Order Status:"+stusVal);
            String strStaus = "";
            if (stusVal == 0) {
                strStaus = "New";
            } else {
                strStaus = "Exported";
            }
          txtOrdNo.setText(transList.get(position).getHhTran_referenceNumber());
            txtOrdDate.setText(strDate);
            txtOrdStatus.setText(strStaus);
        }
        return convertView;
    }

请帮我解决。

1 个答案:

答案 0 :(得分:0)

您只在checked分支中设置convertView == null状态。您没有在else分支中进行设置,因此会保留已回收复选框的已检查状态。

只需移动将复选框选中状态设置为if - else的代码,以便为新视图和回收视图设置。