【问题标题】:Access Child Item's individual ojects in Expandable Listview访问 Expandablelistview 中的子项单个对象
【发布时间】:2015-01-10 22:18:17
【问题描述】:

如何在可展开列表视图的任何组视图中访问子项的子视图??

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 是可展开列表视图上方的另一个复选框。

【问题讨论】:

    标签: android expandablelistview


    【解决方案1】:

    你需要做一个类来扩展BaseExpandableListAdapter,然后重写抽象的getChildView()方法,这个方法是根据groupPosition和childPosition来决定显示哪个view。你也可以访问这个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。

    【讨论】:

    • 我知道,但我的问题是假设我有一个可扩展的列表视图,其中每个子视图都包含一个复选框和一个文本视图。现在如何在列表展开或折叠时访问复选框对象??
    • Thnx...我还是有问题。就像我在列表上方有一个全选复选框。当全选选中时,所有复选框的状态都应相应更改。现在借助链接,我可以更改复选框的状态,但它不会反映在列表中。可以你解释一下为什么??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    相关资源
    最近更新 更多