检查ExpandableListView中的组是否包含项目

时间:2015-03-10 15:39:46

标签: android

如何检查ExpandableListView中的某个组是否包含项目?

我想在用户点击要展开的群组后进行验证 这是我的代码:

expListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
        int count = parent.getChildCount();

        Toast.makeText(getApplicationContext(), count, Toast.LENGTH_SHORT).show();

        return false;
    }
});

我已尝试使用getChildCount(),但我总是收到此错误:

03-10 15:36:23.175    4248-4248/pt.test_pro_gold E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.content.res.Resources$NotFoundException: String resource ID #0xa
            at android.content.res.Resources.getText(Resources.java:229)
            at android.widget.Toast.makeText(Toast.java:265)
            at pt.test_pro_gold.ui.activities.Menu$2.onGroupClick(Menu.java:66)

2 个答案:

答案 0 :(得分:1)

您应该能够使用ExpandableListViewAdapter获取此数字,如下所示:

int count = parent.getExpandableListAdapter().getChildrenCount();

答案 1 :(得分:0)

好人啊!

上面的代码中有一个小错误,方法getChildrenCount必须接受组位置作为参数,如documentation中所述。

您必须实现ExpandableListView.OnGroupClickListener接口并覆盖方法onGroupClick

在下面找到一个示例:

    @Override
    public boolean onGroupClick(ExpandableListView parent, View view, int groupPosition, long id) {
        int noChild = 0;

        int childCount = parent.getExpandableListAdapter().getChildrenCount(groupPosition);
        if (childCount == noChild) {
            Toast.makeText(this, "There is no 'Child' for this 'Group'", Toast.LENGTH_SHORT).show();
        }
        return false;
    }

希望它对其他人有用。