在Android中滚动列表时,已取消选中已单击的复选框

时间:2015-11-28 07:58:40

标签: android

在我的应用程序中,我有一个联系人列表...通过单击复选框,我可以添加联系人...在更新联系人时,我从数据库中获取已经检查过的复选框值以显示已经点击的检查框到用户...所以我从数据库中获取eventMO ...滚动列表时检查未选中,然后下次检查...这就像创建问题...

这是我的代码。请帮我找出解决方案

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

            ViewHolder holder = null;
            // Create a new row view
            if (convertView == null) {

                mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.contactlist, null);
                holder = new ViewHolder();
                // Find the child views.
                holder.userNameTxtV = (TextView) convertView.findViewById(R.id.text);
                holder.mobileNumTxtV = (TextView) convertView.findViewById(R.id.mobile_number);
                holder.cb = (CheckBox) convertView.findViewById(R.id.check_box);

                // Optimization: Tag the row with it's child views, so we don't have
                // to
                // call findViewById() later when we reuse the row.

                convertView.setTag(holder);
            }
            //To reuse already existing rows
            else {
                holder = (ViewHolder) convertView.getTag();
                EventMO eventMO2 = (EventMO) getIntent().getParcelableExtra("eventMO");
                if (eventMO2 != null) {
                    if (userMOs.size() >= eventUserRelationMOs.size()) {
                        if (position <= eventUserRelationMOs.size() - 1) {
                            List<EventUserRelationMO> eventUserRelationMOs = eventMO2.getEventUserRelationBOs();
                            int isInvited = eventUserRelationMOs.get(position).getIsInvited();

                            if (isInvited == 1) {
                                holder.cb.performClick();
                            }
                        }
                    }

                }
            }
            holder.cb.setTag(userMOs.get(position));
            holder.userNameTxtV.setText(userMOs.get(position).getUserName());
            holder.mobileNumTxtV.setText(userMOs.get(position).getMobileNumber());
            holder.cb.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    CheckBox checkBox = (CheckBox) v;
                    UserMO userMO = (UserMO) checkBox.getTag();
                    userMO.setSelected(checkBox.isChecked());
                }
            });
            EventMO eventMO2 = (EventMO) getIntent().getParcelableExtra("eventMO");
            if (eventMO2 != null) {
                if (userMOs.size() >= eventUserRelationMOs.size()) {
                    if (position <= eventUserRelationMOs.size() - 1) {
                        List<EventUserRelationMO> eventUserRelationMOs = eventMO2.getEventUserRelationBOs();
                        int isInvited = eventUserRelationMOs.get(position).getIsInvited();

                        if (isInvited == 1) {
                            holder.cb.performClick();
                        }
                    }
                }

            }
            holder.cb.setChecked(userMOs.get(position).isSelected());
            return convertView;

        }

    }

1 个答案:

答案 0 :(得分:0)

尝试致电

    holder.cb.setOnClickListener(null);

    holder = (ViewHolder) convertView.getTag();
    EventMO eventMO2 = (EventMO) getIntent().getParcelableExtra("eventMO");

    if (eventMO2 != null) {

在你的“其他”区块中。

其他方式是替换这部分:

if (isInvited == 1) {
    holder.cb.performClick();
}

用这个:

 holder.cb.setChecked(isInvited == 1);