java - 使用 ExpandableListView 处理空组点击

标签 java android expandablelistview expandablelistadapter

我正在使用 ExpandableListView 创建一个抽屉导航,但我不知道如何处理空组点击。

每次我尝试单击空组时,都会收到 NullPointerException,显示“尝试在空对象引用上调用接口(interface)方法 '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;
}
}

如有任何帮助,我们将不胜感激。

最佳答案

您调用 size() 的列表为空,因此您必须首先检查该列表。

试试这个:

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

关于java - 使用 ExpandableListView 处理空组点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33203784/

相关文章:

java - Android QR 扫描仪 : How to quit ZXingScannerView. ResultHandler 回到我来自的地方

android - 为什么Android 12不会记录旅行?

Android 布局限定符不起作用?

android - 多级 View : expandablelistview

android - 适用于 Android 的单声道 - ExpandableListView

java - 当我尝试在下面的代码中打印为 RSA 生成的公钥和私钥时,我得到了什么?

java - 使用 hibernate 检查数据库重复的优化方法

java - JAX-RS/ Jersey +文本/xml : can I specify a xml-stylesheet?

java - 带/不带 baseObservable 的 android viewmodel

android - 折叠可扩展 ListView 后如何保留子选择