在Expandablelistview中访问子项目的各个对象

时间:2014-11-13 17:10:58

标签: android expandablelistview

如何在expandable listview中访问任何组视图中子项的子视图?

selectAll.setOnClickListener(new View.OnClickListener(){
@Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
             int gourpsSum = adapter.getGroupCount();  
             for(int i = 0; i < gourpsSum; i++) {  
                 int childSum = adapter.getChildrenCount(i);
                 for(int k = 0; k < childSum;k++) {  
                     boolean isLast = false;  
                     if (k == (childSum - 1)){   
                         isLast = true;  
                     }  

                     CheckBox cBox = (CheckBox) adapter.getChildView(i, k, isLast, null, null).findViewById(R.id.checkBox);  
                     cBox.setChecked(selectAll.isChecked());
                     ((BaseExpandableListAdapter) adapter).notifyDataSetChanged(); 
                 }  

             }  
        }  
});

其中selectAll是可扩展列表视图上方的另一个复选框。

1 个答案:

答案 0 :(得分:1)

您需要创建一个类来扩展BaseExpandableListAdapter,然后覆盖抽象getChildView()方法,此方法基于groupPosition和childPosition来确定要显示的视图。您也可以访问此Expandable List View tutorial

在本教程中,它展示了如何在每个子视图中访问TextView对象,如果要访问CheckBox,可以执行类似的操作。以下是示例代码:

    @Override
    public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);
        CheckBox checkboxListChild = (CheckBox) convertView
                .findViewById(R.id.checkboxListItem); //assume thats the id of your checkbox object


        txtListChild.setText(childText);
        return convertView;
    }

您还可以通过复选框查看this tutorial以了解ExpandableListView。

相关问题