如何从 ExpandableListView 中的组中获取项目?

How to reach the item from a group in an ExpandableListView?

我有一个 OnGroupClick 方法来查看 ExpandableListView 中的特定组是否是一个组。组的模型有一个 ChildItem 列表,当它为 null 时,ExpandableListView 不能尝试扩展,而只是执行该 GroupItem 的特定代码。

我有这个有效的代码:

mDrawerList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        if (groupPosition == 0 || groupPosition == 3) {
            Toast.makeText(MainActivity.this, "Start clicked", Toast.LENGTH_LONG).show();
            return true;
        } else
            return false;
    }
});

我试过的是下面的代码:

mDrawerList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        JPPNavDrawerAdapter2 adapter = (JPPNavDrawerAdapter2) parent.getAdapter();
        JPPNavDrawerAdapter2.GroupItem item = (JPPNavDrawerAdapter2.GroupItem) adapter.getGroup(groupPosition);
        if (item.getChildren() == null) {
            Toast.makeText(MainActivity.this, "Start clicked", Toast.LENGTH_LONG).show();
            return true;
        } else
            return false;
    }
});

当我 运行 执行此操作时,我的应用程序会在我尝试单击某个项目时崩溃。这段代码有什么问题?我该如何解决这个问题?

getAdapter() 方法不会 return 您使用 setAdapter() 定义的适配器实例。您必须改用 getExpandableListAdapter()。文档 clearly states this.