使用ExpandableListView

时间:2015-10-18 22:04:40

标签: java android expandablelistview expandablelistadapter

我正在创建一个带有ExpandableListView的导航抽屉,但我无法弄清楚如何处理空组点击。

每当我尝试点击一个空组时,我得到一个NullPointerException,它说“尝试在空对象引用上调用接口方法'int java.util.List.size()'并且它指向getChildrenCount(方法。

这是我的自定义ExpandableListAdapter:

ExpandableListAdapter.java

package co.eshg.drawertest;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context context;
private List<String> listDataItem; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> listDataChild;

public ExpandableListAdapter(Context context, List<String> listDataItem,
                             HashMap<String, List<String>> listChildData) {
    this.context = context;
    this.listDataItem = listDataItem;
    this.listDataChild = listChildData;
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this.listDataChild.get(this.listDataItem.get(groupPosition))
            .get(childPosititon);
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public View getChildView(int groupPosition, final int childPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    final String childText = (String) getChild(groupPosition, childPosition);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.drawer_child_item, null);
    }

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.tvChildItem);

    txtListChild.setText(childText);
    return convertView;
}

@Override
public int getChildrenCount(int groupPosition) {
    if (this.listDataChild.get(this.listDataItem.get(groupPosition)).size() != 0) {
        return this.listDataChild.get(this.listDataItem.get(groupPosition)).size();
    }
    return 1;
}

@Override
public Object getGroup(int groupPosition) {
    return this.listDataItem.get(groupPosition);
}

@Override
public int getGroupCount() {
    return this.listDataItem.size();
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) this.context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.drawer_list_item, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.tvListItem);
    lblListHeader.setText(headerTitle);

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}
}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您呼叫size()的列表为空,因此您必须先检查该列表。

试试这个:

@Override
public int getChildrenCount(int groupPosition) {
    List childList = listDataChild.get(listDataItem.get(groupPosition));
    if (childList != null && ! childList.isEmpty()) {
        return childList.size();
    }
    return 1;
}