Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android ExpandableListView getChildView未触发_Java_Android_Expandablelistview - Fatal编程技术网

Java Android ExpandableListView getChildView未触发

Java Android ExpandableListView getChildView未触发,java,android,expandablelistview,Java,Android,Expandablelistview,我做了一个ExpandableListView,下面有很多教程和SO提出的一些错误问题。 父列表由两个文本视图和一个带有标准布局的ImageButton组成。子列表由单个文本视图组成。 数据源来自活动的SQLite数据库,并生成ArrayList 问题: 不知何故,父组列表正常工作,即使按钮正常工作,但当我尝试展开时,它不会展开。因此,我尝试放置一些Toast消息,但在打开活动之后,它看起来像getChildView()方法根本没有触发,因为Toast消息根本没有显示 ExpandableLis

我做了一个ExpandableListView,下面有很多教程和SO提出的一些错误问题。 父列表由两个文本视图和一个带有标准布局的ImageButton组成。子列表由单个文本视图组成。 数据源来自活动的SQLite数据库,并生成ArrayList

问题:

不知何故,父组列表正常工作,即使按钮正常工作,但当我尝试展开时,它不会展开。因此,我尝试放置一些Toast消息,但在打开活动之后,它看起来像
getChildView()
方法根本没有触发,因为Toast消息根本没有显示

ExpandableListAdapter类如下所示

public class InventoryListExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Inventory> data;
    private HashMap<String, ArrayList<String>> list_data;

    public InventoryListExpandableListAdapter(Context context, ArrayList<Inventory> data, HashMap<String, ArrayList<String>> list_data) {
        this.context = context;
        this.data = data;
        this.list_data = list_data;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {return list_data.get(data.get(groupPosition)).size();}

    @Override
    public Inventory getGroup(int groupPosition) {return data.get(groupPosition);}

    @Override
    public Object getChild(int groupPosition, int childPosition) {return list_data.get(data.get(groupPosition).getCode()).get(childPosition);}

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

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

    @Override
    public boolean hasStableIds() {return true;}

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            final Inventory inventory = getGroup(groupPosition);

            convertView = View.inflate(context, R.layout.listview_layout_two_row_delete, null);
            ImageButton btnDelete = (ImageButton)convertView.findViewById(R.id.btnDelete);
            TextView text1 = (TextView)convertView.findViewById(R.id.text1);
            TextView text2 = (TextView)convertView.findViewById(R.id.text2);

            text1.setText(inventory.getCode());
            text2.setText(inventory.getItem());

            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
                }
            });
        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String)getChild(groupPosition, childPosition);
        LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {
            convertView = infalInflater.inflate(R.layout.listview_layout_single_text, null);
        }
        Toast.makeText(context, "Make Child number "+String.valueOf(childPosition), Toast.LENGTH_SHORT).show();

        TextView text1 = (TextView)convertView.findViewById(R.id.text1);
        text1.setText(childText);
        return convertView;
    }

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

}
公共类InventoryListExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有数组列表数据;
私有HashMap列表_数据;
公共清单ListExpandableListAdapter(上下文上下文、ArrayList数据、HashMap列表数据){
this.context=上下文;
这个数据=数据;
this.list\u data=list\u data;
}
@凌驾
public int getGroupCount(){return data.size();}
@凌驾
public int getChildrenCount(int groupPosition){返回列表_data.get(data.get(groupPosition)).size()}
@凌驾
公共资源清册getGroup(int groupPosition){返回数据。get(groupPosition);}
@凌驾
公共对象getChild(int-groupPosition,int-childPosition){return list_data.get(data.get(groupPosition.getCode()).get(childPosition);}
@凌驾
public long getGroupId(int groupPosition){return groupPosition;}
@凌驾
public long getChildId(int-groupPosition,int-childPosition){return-childPosition;}
@凌驾
公共布尔hasStableIds(){return true;}
@凌驾
公共视图getGroupView(最终int groupPosition、布尔值isExpanded、视图convertView、视图组父级){
if(convertView==null){
最终库存=getGroup(groupPosition);
convertView=View.inflate(上下文,R.layout.listview\u布局\u两行\u删除,null);
ImageButton btnDelete=(ImageButton)convertView.findViewById(R.id.btnDelete);
TextView text1=(TextView)convertView.findViewById(R.id.text1);
TextView text2=(TextView)convertView.findViewById(R.id.text2);
text1.setText(inventory.getCode());
text2.setText(inventory.getItem());
btnDelete.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Toast.makeText(上下文“已删除”,Toast.LENGTH_SHORT).show();
}
});
}
返回视图;
}
@凌驾
公共视图getChildView(int-groupPosition、int-childPosition、布尔isLastChild、视图convertView、视图组父级){
最终字符串childText=(字符串)getChild(groupPosition,childPosition);
LayoutInflater INFLATER=(LayoutInflater)this.context.getSystemService(context.LAYOUT\u INFLATER\u SERVICE);
if(convertView==null){
convertView=infalInflater.flate(R.layout.listview\u layout\u single\u text,null);
}
Toast.makeText(上下文,“生成子编号”+String.valueOf(childPosition),Toast.LENGTH_SHORT.show();
TextView text1=(TextView)convertView.findViewById(R.id.text1);
text1.setText(childText);
返回视图;
}
@凌驾
公共布尔值isChildSelectable(int-groupPosition,int-childPosition){
返回true;
}
}

我感谢你给我的任何帮助。谢谢。

更改hasStableIds方法

  @Override
    public boolean hasStableIds() {return false;}
**请更改getGroupView方法::**

 @Override
 public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            final Inventory inventory = getGroup(groupPosition);

            convertView = View.inflate(context, R.layout.listview_layout_two_row_delete, null);
            }
            ImageButton btnDelete = (ImageButton)convertView.findViewById(R.id.btnDelete);
            TextView text1 = (TextView)convertView.findViewById(R.id.text1);
            TextView text2 = (TextView)convertView.findViewById(R.id.text2);

            text1.setText(inventory.getCode());
            text2.setText(inventory.getItem());

            btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
                }
            });

        return convertView;
    }

我花了几个小时试图找到同样的问题,但却发现这对我来说非常简单:

  • 您在GroupView中有一个ImageButton,它正在拦截触摸以扩展视图。使用此选项来修复它:
  • android:focusable=“false” android:focusableInTouchMode=“false”

  • 由于组视图未展开,因此没有理由浪费CPU周期来获取不会显示的视图
  • 适配器中与子级相关的所有方法都不会被调用

  • 尝试调用expandableListView.expandGroup(0…n)以手动展开它,并查看它是否调用了适配器中与子级相关的方法在ExpandedListAdapter类中未自动调用时调用。这是一个似乎可以解决问题的快捷方式。我有一个扩展的列表视图,父组项是一个复选框列表。单击复选框可显示子项。下面是我如何编写getGroupView()函数的

    @Override
    public View getGroupView(final int groupPosition, final boolean isExpanded, View convertView, final ViewGroup viewGroup)
    {
        if (convertView == null)
        {
            LayoutInflater inflater = (LayoutInflater) m_context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.expandable_list_milk_type,null);
        }
        final CheckBox cbMilkType = (CheckBox) convertView.findViewById(R.id.cb_milk_type);
        cbMilkType.setText(m_listMilktype.get(groupPosition));
    
        cbMilkType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ExpandableListView expandableListView = (ExpandableListView)viewGroup;
                cbMilkType.toggle();
                if (!isExpanded) {
                    expandableListView.expandGroup(groupPosition);
                }
                else {
                    expandableListView.collapseGroup(groupPosition);
                }
            }
        });
        return convertView;
    }
    

    如果它没有被调用,可能是因为
    getChildrenCount
    返回0,我已经检查了它,但大小不是0。此解决方案对我有效。非常感谢你。它快把我逼疯了!