使用复选框扩展列表视图

时间:2014-04-23 00:51:36

标签: android expandablelistview

我正在使用复选框处理可扩展列表视图。孩子的所有观点都是正确的。问题是,如果选中复选框,则会随机检查子视图,就像我展开groupview和子视图一样,如果澄清了isssue绑定,则标签不会正确地添加到复选框中。

enter code here
             if (row == null)
     {
        holder=new ChildHolder();
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = infalInflater.inflate(R.layout.list_item_msg, null);
            holder. txtListChild = (TextView)                row.findViewById(R.id.msgtextID);
          holder.sendnow=(CheckBox)row.findViewById(R.id.sendnowID);
         list=_listDataChild.get(_listDataHeader.get(groupPosition)).get(childPosition);
        notifyDataSetChanged();


  holder.sendnow.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
                      // ch.setChecked(buttonView.isChecked());  
                notifyDataSetChanged();
                // getPosition = (Integer) buttonView.getTag();

            if(isChecked)
            { 
                 contactsVo childText = (contactsVo) holder.sendnow.getTag();
                  childText = (contactsVo) getChild(groupPosition, childPosition);
                childText.setChecked(buttonView.isChecked());
                personals=new ArrayList<contactsVo>();
                set.add(list.getFileId());
                contactsVo obj1=new contactsVo();
                obj1.setImeiNo(list.getImeiNo());
                obj1.setUserId(list.getUserId());
                //obj.add(obj1);
                personals.add(obj1);
                Log.e("=================","========================");

            }else
            {
                Log.e("Uncheked","See it");
                set.remove(list.getFileId());

            }
        }
    });
  row.setTag(holder);
    holder.sendnow.setTag((contactsVo) getChild(groupPosition,childPosition));

    }else
    {
        row = convertView;
         ((ChildHolder) row.getTag()).sendnow.setTag((contactsVo) getChild(groupPosition,childPosition));
    }

    try
    {
         ChildHolder holder = (ChildHolder)row.getTag();
         childText = (contactsVo) getChild(groupPosition,childPosition);
         holder.txtListChild.setText(childText.getName());
     holder.sendnow.setChecked(((contactsVo) getChild(groupPosition,childPosition)).isChecked());


    }catch(Exception e)
    {
        e.printStackTrace();
    }

2 个答案:

答案 0 :(得分:0)

您应该在更改适配器后调用notifyDataSetChanged();,以便您的代码在下面给出。

 holder=new ChildHolder();
    LayoutInflater infalInflater = (LayoutInflater) this._context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    row = infalInflater.inflate(R.layout.list_item_msg, null);
        holder. txtListChild = (TextView)                row.findViewById(R.id.msgtextID);
      holder.sendnow=(CheckBox)row.findViewById(R.id.sendnowID);
     list=_listDataChild.get(_listDataHeader.get(groupPosition)).get(childPosition);
     // notifyDataSetChanged();


 holder.sendnow.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
                  // ch.setChecked(buttonView.isChecked());  

            // getPosition = (Integer) buttonView.getTag();

        if(isChecked)
        { 
             contactsVo childText = (contactsVo) holder.sendnow.getTag();
              childText = (contactsVo) getChild(groupPosition, childPosition);
            childText.setChecked(buttonView.isChecked());
            personals=new ArrayList<contactsVo>();
            set.add(list.getFileId());
            contactsVo obj1=new contactsVo();
            obj1.setImeiNo(list.getImeiNo());
            obj1.setUserId(list.getUserId());
            //obj.add(obj1);
            personals.add(obj1);
            Log.e("=================","========================");

        }else
        {
            Log.e("Uncheked","See it");
            set.remove(list.getFileId());

        }

        notifyDataSetChanged();

    }
});

希望它适合你。

答案 1 :(得分:0)

列表视图中的复选框可能会让人感到困惑,您无法依赖视图来了解是否已经选中了一个框,您需要创建与您的适配器中的项目大小相同的布尔引用并设置复选框手动取决于其是真还是假。由于视图在列表视图中被回收并且您滚动列表,因此即使您尚未检查它们,也会检查某些视图。不要设置已检查的更改侦听器,因为滚动时会触发。这是一个我用复选框的适配器,每次调用getview你需要引用布尔数组来确定你是否要检查该框

class TagAdapter extends StandardAdapter {

boolean[] checkedItems;
int iconWidth;

public TagAdapter() {

    checkedItems = new boolean[items.size()];

    registerDataSetObserver(new DataSetObserver() {

        @Override
        public void onChanged() {
            super.onChanged();
            updateProperties();
        }

    });

    iconWidth = MyApp.getScreenWidth() / 7;
}

void updateProperties() {
    checkedItems = new boolean[items.size()];
}

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

    final ViewHolder viewHolder;

    if (convertView == null) {

        convertView = mLayoutInflater.inflate(R.layout.checkin_friend, parent, false);

        viewHolder = new ViewHolder();

        viewHolder.ivIcon = (CachedImageView) convertView.findViewById(R.id.ivIcon);
        viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
        viewHolder.cbFriend = (CheckBox) convertView.findViewById(R.id.cbFriend);

        convertView.setTag(viewHolder);

    }
    else {
        viewHolder = (ViewHolder) convertView.getTag();
    }

    final JsonObject item = getItem(position);
    final Profile profile = new Gson().fromJson(item, Profile.class);

    viewHolder.tvName.setText(profile.getUsername());

    if (profile.getIcon() != null) {

        viewHolder.ivIcon.set(app.imageContainer + profile.getIcon(), true);

    }
    else {
        viewHolder.ivIcon.setImageDrawable(app.defaultIcon);

    }

    viewHolder.ivIcon.setLayoutParams(new RelativeLayout.LayoutParams(iconWidth, iconWidth));



    viewHolder.cbFriend.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


        }
    });

    viewHolder.cbFriend.setChecked(checkedItems[position]);

    convertView.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            boolean current = checkedItems[position];

            if (current == true) {

                boolean modified = tags.remove(profile);

                if(modified == false){
                    Log.d(MyApp.TAG,"modification was false");
                }
            }
            else {
                tags.add(profile);
            }

            checkedItems[position] = !current;

            viewHolder.cbFriend.setChecked(checkedItems[position]);

            updateUi();

        }
    });



    return convertView;

}

public class ViewHolder {
    CachedImageView ivIcon;
    TextView tvName;
    CheckBox cbFriend;
}

}