Access Child Item's individual ojects in Expandable Listview

不问归期 提交于 2020-01-05 07:30:54

问题


How do I access the child views of child items in any group view in 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(); 
                 }  

             }  
        }  
});

where selectAll is another Checkbox above the expandable listview.


回答1:


You need to make a class to extend the BaseExpandableListAdapter, and then override the abstract getChildView() method, this method is based on groupPosition and childPosition to determine which view to be displayed. You can also visit this Expandable List View tutorial.

In the tutorial, it shows how to access the TextView object in each child view, if you want to access the CheckBox, you can do the similar thing. Below is a sample code:

    @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;
    }

You can also check out this tutorial to learn about ExpandableListView with checkbox.



来源:https://stackoverflow.com/questions/26914488/access-child-items-individual-ojects-in-expandable-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!