2013-12-15 97 views
2

我想在android應用程序中創建一個簡單的ExpandableListView。使用HashMap創建ExpandableListView的錯誤<String,List <String>>

這是準備用於循環的ExpandableListView.I數據的代碼,因爲在我的應用程序中,這些數據在運行時即將到來,所以我無法識別它將持續多久。

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這個應用程序標題出現完美的,但是當我在特定的標題單擊該應用程序失敗。

這裏是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異常,但無法瞭解如何使它正確,以便它在一個標題顯示一個一個刺?

回答

1

適配器中的getChildrenCount()方法返回無效值。

無效值表示該值可能大於實際大小或小於實際大小。

它首先在getChildernCount()和getChild()方法上工作。

在我的代碼中,我忘了糾正getChilderCount()方法中的代碼。

相關問題