“消耗品列表”中所选项目列表复选框

时间:2015-12-29 08:14:07

标签: android expandablelistview expandablelistadapter

我想从消耗品列表视图中获取所选数据项。首先,我得到了所有元素,无论我是否选择了它们......但我无法获得所选项目。并将其作为父母及其孩子展示。

public class CategoryBaseAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<ProfessionEntity> _listDataHeader;
public static HashMap<ProfessionEntity, List<ProfessionChildEntity>> _listDataChild;
public static HashMap<Integer, boolean[]> mChildCheckStates;
    private Map<ProfessionEntity, List<ProfessionChildEntity>> selectedItems;
public CategoryBaseAdapter(Context context,
        List<ProfessionEntity> listDataHeader,
        HashMap<ProfessionEntity, List<ProfessionChildEntity>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
    selectedItems = new HashMap<ProfessionEntity,List<ProfessionChildEntity>>();
}
public Map<ProfessionEntity, List<ProfessionChildEntity>> getSelectedItems() {
    return selectedItems;
}
public boolean isSelected(int groupPosition, int childPosition){
    ProfessionEntity group = _listDataHeader.get(groupPosition);
    ProfessionChildEntity child = getChild(groupPosition,childPosition);
    List<ProfessionChildEntity> sel = selectedItems.get(group);
    return sel != null && sel.contains(child);
}
public void toggleSelection(int groupPosition, int childPosition){
    ProfessionEntity group = _listDataHeader.get(groupPosition);
    ProfessionChildEntity child = getChild(groupPosition,childPosition);

    List<ProfessionChildEntity> sel = selectedItems.get(group);
    if (sel == null){
        sel = new ArrayList<ProfessionChildEntity>();
        selectedItems.put(group, sel);
    }

    if (sel.contains(child))
        sel.remove(child);
    else
        sel.add(child);

}

@Override
public ProfessionChildEntity getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(final int groupPosition, int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {
    final int mGroupPosition = groupPosition;
    final int mChildPosition = childPosition;
    final String childText = getChild(groupPosition, childPosition)
            .getDescription();
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.child_item, null);

    }
    final CheckBox childCheckBox = (CheckBox) convertView
            .findViewById(R.id.childCheckBox);
    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.childTextView);
    txtListChild.setText(childText);
    childCheckBox.setOnCheckedChangeListener(null);         
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .size();
}

@Override
public ProfessionEntity getGroup(int groupPosition) {
    return this._listDataHeader.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this._listDataHeader.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {
    String headerTitle = getGroup(groupPosition).getDecription();
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.group_item, null);
    }
    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.groupTextView);
    CheckBox grpCheckBox = (CheckBox) convertView
            .findViewById(R.id.groupCheckBox);
    grpCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            Log.d("grp clicked", "grpCheckBox");
        }
    });
    lblListHeader.setText(headerTitle);
    grpCheckBox.setTag(convertView);
    if (isExpanded) {
        Log.d("abc", "abc");
    } else {
        Log.d("abc", "abc");
    }
    return convertView;
}
@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

1 个答案:

答案 0 :(得分:0)

getGroupView()方法中你应该试试这个:

    @Override
        public View getGroupView(int groupPosition, boolean isExpanded,
                View convertView, ViewGroup parent) {
        GroupHolder groupHolder;
            String headerTitle = getGroup(groupPosition).getDecription();
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.group_item, null);
                groupHolder = new GroupHolder();
                 groupHolder.lblListHeader = (TextView) convertView
                    .findViewById(R.id.groupTextView);
                groupHolder.grpCheckBox = (CheckBox) convertView
                    .findViewById(R.id.groupCheckBox);
        convertView.setTag(groupHolder);
            }
        else
        {
        groupHolder = (GroupHolder) convertView.getTag();
        }

         groupHolder.grpCheckBox.setOnClickListener(new View.OnClickListener() 
        {

                @Override
                public void onClick(View v) 
                {
                    new onCheckBoxClick() 
                    {

                        @Override
                        public void OnCheckClick(boolean isChecked) 
                        {
                            groupHolder.grpCheckBox.setChecked(isChecked);
                        }
                    };
                }
            });
           groupHolder.lblListHeader.setText(headerTitle);
// you can get data from your modal class i.e.ProfessionEntity class which you have to show check box checked or not checked the following line is for your understanding.           
groupHolder.grpCheckBox.setChecked(groupHolder.grpCheckBox.getChecked());    

            return convertView;
        }

        private class GroupHolder
        {
                TextView lblListHeader
                CheckBox grpCheckBox;

        }

        private interface onCheckBoxClick
        {
                void OnCheckClick(boolean isChecked);
        }

如果您有任何疑问,请告诉我