2013-04-22 68 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