Android可扩展ListView子项不显示

时间:2017-03-21 06:45:34

标签: android listview expandablelistview expandablelistadapter

我想在我的项目中制作一个可扩展的列表视图。

ExpandableListViewAdapter.Java

public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<itemGroupAddon> _listDataHeader;

private HashMap<List<itemGroupAddon>, List<ItemAddon>> _listDataChild;

public ExpandableListAdapter(Context context, List<itemGroupAddon> listDataHeader,
                             HashMap<List<itemGroupAddon>, List<ItemAddon>> listChildData) {
    this._context = context;
    this._listDataHeader = listDataHeader;
    this._listDataChild = listChildData;
}

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

@Override
public int getChildrenCount(int groupPosition) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();
}

@Override
public Object getChild(int groupPosition, int childPosititon) {
    return this._listDataChild.get(this._listDataHeader.get(groupPosition))
            .get(childPosititon).getNama_addon();
}

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

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

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

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

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

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

    return convertView;
}

@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 infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item_mod, null);
    }

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

    txtListChild.setText(childText);
    return convertView;
}

@Override
public boolean isChildSelectable(int i, int i1) {
    return false;
}

}

这是我在Activity中调用数据的过程。我从我的sqlite数据库中获取数据。

public void PrepareListData(int itemID,int OutletID)
{
    listHeaderData = new ArrayList<>();
    listData = new ArrayList<>();
    listDataChild = new HashMap<List<itemGroupAddon>, List<ItemAddon>>();
    listHeaderData = db.getAllItemGroup(itemID);

    for(itemGroupAddon items : listHeaderData){
        listData= db.getAddonItemList(items.getItem_addon_group_id(),outletID);
    }

    listDataChild.put(listHeaderData,listData);
}

在我的OnCreate中我称之为

PrepareListData(itemId,outletID);
adapterExpList = new ExpandableListAdapter(getActivity(), listHeaderData, listDataChild);
 lvModifier.setAdapter(adapterExpList);

当我运行程序时,它只显示标题,但子数据没有显示。当我单击标题以展开childData时,我收到致命错误

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.project.pos.posandroid, PID: 28411
              java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
                  at com.project.pos.posandroid.Adapter.ExpandableListAdapter.getChildrenCount(ExpandableListAdapter.java:41)
                  at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563)
                  at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688)
                  at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:696)
                  at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:656)
                  at android.widget.AbsListView$PerformClick.run(AbsListView.java:3066)
                  at android.widget.AbsListView.onTouchUp(AbsListView.java:3915)
                  at android.widget.AbsListView.onTouchEvent(AbsListView.java:3680)
                  at android.view.View.dispatchTouchEvent(View.java:9333)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2241)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                  at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                  at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                  at android.app.Dialog.dispatchTouchEvent(Dialog.java:787)
                  at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                  at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                  at android.view.View.dispatchPointerEvent(View.java:9553)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4248)
                  at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4101)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3647)
                  at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3700)
                  at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3666)
                  at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3792)
                  at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3674)
                  at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3849)
                  at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3647)
                at android.view.ViewRootImpl$InputStage

错误显示在我的适配器的这一部分

return this._listDataChild.get(this._listDataHeader.get(groupPosition)).size();

请帮我解决这个问题。先谢谢

1 个答案:

答案 0 :(得分:0)

我认为问题出在这一行listDataChild = new HashMap<List<itemGroupAddon>, List<ItemAddon>>();,你正在用List&lt;&gt;创建一个HashMap。因为您的列表是可变的,所以不是定义HashMap的正确方法的键。见ArrayList as key in Hashmap

在查询db

后,还要检查listData是否为空
listData= db.getAddonItemList(items.getItem_addon_group_id(),outletID);