【问题标题】:Expandable listview without children没有子项的可扩展列表视图
【发布时间】:2013-04-22 17:30:47
【问题描述】:

我是 android 开发新手,也是第一次使用可扩展列表视图。我正在创建一个应用程序,在其中我从 web 服务获取 d 可扩展列表视图的所有内容。我也从 wevservice 获取所有内容,但是当可扩展列表视图中没有父级的子级时,可扩展列表视图 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();
}

【问题讨论】:

    标签: android nullpointerexception expandablelistadapter


    【解决方案1】:

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

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

    【讨论】:

    • 你能告诉我们应该在哪里初始化这个空的子数组吗?我也面临同样的问题
    • @Alexandar Zhak 请回复
    • @Sharp edge 在您的活动的 onCreate() 中,您初始化 ExpandableListView 并将其设置为适配器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    相关资源
    最近更新 更多