我有问题。单击组时,我的可扩展列表视图会产生错误(要打开组并显示子组)。这是log cat结果。
09-24 01:44:47.987: E/AndroidRuntime(15754): FATAL EXCEPTION: main
09-24 01:44:47.987: E/AndroidRuntime(15754): Process: jkos.development.sioba, PID: 15754
09-24 01:44:47.987: E/AndroidRuntime(15754): java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
09-24 01:44:47.987: E/AndroidRuntime(15754): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
09-24 01:44:47.987: E/AndroidRuntime(15754): at java.util.ArrayList.get(ArrayList.java:308)
09-24 01:44:47.987: E/AndroidRuntime(15754): at jkos.development.sioba.adapter.TumbuhanObatDetailAdapter.getChild(TumbuhanObatDetailAdapter.java:49)
09-24 01:44:47.987: E/AndroidRuntime(15754): at jkos.development.sioba.adapter.TumbuhanObatDetailAdapter.getChildView(TumbuhanObatDetailAdapter.java:95)
09-24 01:44:47.987: E/AndroidRuntime(15754): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:451)
09-24 01:44:47.987: E/AndroidRuntime(15754): at android.widget.AbsListView.obtainView(AbsListView.java:2257)
09-24 01:44:47.987: E/AndroidRuntime(15754): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
09-24 01:44:47.987: E/AndroidRuntime(15754): at android.widget.ListView.onMeasure(ListView.java:1175)
09-24 01:44:47.987: E/AndroidRuntime(15754): at android.view.View.measure(View.java:16525)
这是我的适配器类
public class TumbuhanObatDetailAdapter extends BaseExpandableListAdapter {
private Context _context;
private ArrayList<String> listHeader;
private HashMap<String, List<String>> listItem;
public TumbuhanObatDetailAdapter(Context context, ArrayList<String> listHeader, HashMap<String, List<String>> listItem){
this._context = context;
this.listHeader = listHeader;
this.listItem = listItem;
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return this.listHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this.listItem.size();
}
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return this.listHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
//return this._listItem.get(this._listHeader.get(groupPosition)).get(childPosition);
return this.listItem.get(listHeader.get(groupPosition)).get(childPosition);
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public boolean hasStableIds() {
// TODO Auto-generated method stub
return true;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.tumbuhanobatdetailgroupitem, null);
}
TextView lblListHeader = (TextView) convertView.findViewById(R.id.expandableListViewHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
//return null;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//System.out.println("group : "+groupPosition+" child : "+childPosition);
String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.tumbuhanobatdetailchilditem, null);
}
TextView txtListChild = (TextView) convertView.findViewById(R.id.childItem);
txtListChild.setText(childText);
return convertView;
//return null;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return true;
}
}
这是一个在我的java活动中调用适配器的部分
ArrayList<String>listHeader = new ArrayList<String>();
HashMap<String, List<String>>listItem = new HashMap<String, List<String>>();
listHeader.add("Umum");
listHeader.add("Simplisia");
listHeader.add("Kegunaan Empiris");
List<String> umum = new ArrayList<String>();
umum.add(namaIndonesia);
umum.add(namaLatin);
List<String> simplisia = new ArrayList<String>();
for(int i = 0; i < namaSimplisia.size(); i++){
simplisia.add(namaSimplisia.get(i));
}
List<String> penggunaanEmpiris = new ArrayList<String>();
for(int i = 0; i < kegunaanEmpiris.size(); i++){
penggunaanEmpiris.add(kegunaanEmpiris.get(i));
}
listItem.put(listHeader.get(0), umum);
listItem.put(listHeader.get(1), simplisia);
listItem.put(listHeader.get(2), penggunaanEmpiris);
tumbuhanObatDetailAdapter = new TumbuhanObatDetailAdapter(context, listHeader, listItem);
expandableListView.setAdapter(tumbuhanObatDetailAdapter);
我需要尽快回答
答案 0 :(得分:1)
将您的getChildrenCount()代码更改为:
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return this.listItem.get(groupPosition).size();
}
您将完整hashmap的大小作为特定组中子项的计数。这就是它导致IndexOutOfBoundException的原因。
答案 1 :(得分:0)
数组是0索引的 - 意味着数组包含对象0和对象1,而不是对象1和对象2,所以在调用函数之前只需删除1
更改此
String childText = (String) getChild(groupPosition, childPosition);
到这个
String childText = (String) getChild(groupPosition - 1, childPosition);