【问题标题】:Handling empty group clicks with ExpandableListView使用 ExpandableListView 处理空组点击
【发布时间】:2015-10-18 22:04:39
【问题描述】:

我正在使用 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;
}
}

任何帮助将不胜感激。

【问题讨论】:

    标签: java android expandablelistview expandablelistadapter


    【解决方案1】:

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

    试试这个:

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

    【讨论】:

    • 感谢您的回答。我换掉了代码,它工作了,但它仍然给我一个 NullPointerException,这一次,它指向 getChild() 和 getChildView() 方法。知道如何为这些方法实现相同的 if 语句吗?再次感谢。
    • 那是因为即使子列表为空,您也会返回 1。除非您正在为空列表创建特殊的子视图,否则您应该在列表为空时返回 0。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多