2013-04-22 64 views
9

我是第一次使用可扩展列表视图的Android开发人员。我正在创建一个应用程序,在这里我从webservice获取d可扩展列表视图的所有内容。我从wevservice中获取所有内容,但是当可扩展列表视图中没有父项的子项时,可扩展列表视图的自定义适配器merhod 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(); 
} 
+0

发布您的代码.. – Pragnani 2013-04-22 17:31:34

回答

16

只是初始化一个空儿数组是这样的:

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

能否请你告诉我们仅仅应该在哪里初始化这个空儿阵列?我面临同样的问题 – 2014-08-19 12:45:41

+0

@Alexandar Zhak请回复 – 2014-08-19 13:12:13

+0

@Sharp edge在你的activity的onCreate()中,你初始化ExpandableListView并设置它的Adapter。 – 2014-08-21 21:21:10