可展开的列表视图,但没有子项。

9
我是一名初学Android开发的新手,也是第一次使用可扩展列表视图。我正在创建一个应用程序,在其中从Web服务获取可扩展列表视图的所有内容。我也从webservice获取了所有内容,但当在可扩展列表视图中父项没有子项时,可扩展列表视图的自定义适配器方法getchildcount()会返回null,并引发空指针异常。如何只在列表中显示没有子项的父项以及当两者都可用时怎么办?谢谢。
public View getChildView(int p_id, int c_id, boolean bln1, View view,ViewGroup viewgroup) {
    // TODO Auto-generated method stub

     if (view == null) {
            view = inflater.inflate(com.example.eventlive.R.layout.homescreen_list_item_child, viewgroup,false);
        }

        TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
        //"i" is the position of the parent/group in the list and 
        //"i1" is the position of the child
        textView.setText(mParent.get(p_id).getArrayChildren().get(c_id));
        view.setBackgroundResource(R.drawable.child_bg);
        //return the entire view
        return view;
}

//counts the number of children items so the list knows how many times calls getChildView() method
@Override
public int getChildrenCount(int p_id) {
    // TODO Auto-generated method stub
    return mParent.get(p_id).getArrayChildren().size();
}
1个回答

16

只需像这样初始化一个空的子数组:

private HashMap<String, ArrayList<String>> mGroupsItems =
        new HashMap<String, ArrayList<String>>();
. . .
mGroupsItems.put(name_of_empty_group, new ArrayList<String>());

请问我们应该在哪里初始化这个空的 children 数组呢?我也遇到了同样的问题。 - Sharp Edge
@Alexandar Zhak 请回复。 - Sharp Edge
在您的Activity的onCreate()方法中,初始化ExpandableListView并设置其适配器。 - Alexander Zhak

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接