使用 HashMap<String,List 创建 ExpandableListView 时出错<String>>



我正在尝试在android应用程序中创建一个简单的ExpandableListView。

这是为ExpandableListView准备数据的代码。我在这里使用循环,因为在我的应用程序中,这些数据是在运行时出现的,所以我无法识别它将持续多久。

header = new ArrayList<String>();
child = new HashMap<String, List<String>>();
header.add("Top 250");
header.add("Now Showing");
header.add("Coming Soon");
for (int i = 0; i < 3; i++) {
        List<String> Map = new ArrayList<String>();
        Map.add("Hello:" + i);
        child.put(header.get(i), Map);
}

为了填充我创建的自定义适配器ExpandableListView,下面是代码。

@Override
public Object getChild(int groupPosition, int childPosition) {
    String headerData = this._listDataHeader.get(groupPosition);
    List<String> listData = this._listDataChild.get(headerData);
    return listData.get(childPosition);
}

当我试图运行这个应用程序时,ExpandableListView中的Header看起来很完美,但当我点击特定的Header时,应用程序失败了。

这是logcat。

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
com.kamani.expandablelistdemo.MyCustomAdpater.getChild(MyCustomAdpater.java:36)
com.kamani.expandablelistdemo.MyCustomAdpater.getChildView(MyCustomAdpater.java:51)
android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)

我知道getChild()方法在返回数据时出错,它说IndexOutOfBoundsException,但无法理解如何更正它,使其在一个Header中显示一个Sting

适配器中的getChildrenCount()方法返回无效值

无效值表示该值可能大于实际大小或小于实际大小。

它首先在getChilderCount()上工作,然后在getChild()方法上工作。

在我的代码中,我忘记更正getChilderCount()方法中的代码。

相关内容

最新更新