2015-10-18 25 views
0

我用ExpandableListView創建了一個導航抽屜,但是我無法弄清楚如何處理空的組點擊。用ExpandableListView處理空的組點擊

每次嘗試點擊一個空組時,我都會得到一個NullPointerException異常,它表示「嘗試調用null對象引用上的接口方法'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; 
} 
} 

任何幫助,將不勝感激。

回答

2

您打電話給size()的列表爲空,因此您必須先檢查該列表。

試試這個:

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

感謝您的回答。我換出了代碼並且它可以工作,但它仍然給我一個NullPointerException,這次它指向了getChild()和getChildView()方法。任何想法如何我可以爲這些方法實現相同的if語句?再次感謝。 – user5460905

+0

這是因爲即使孩子列表爲空,您仍然返回1。除非您爲空列表創建特殊的子視圖,否則當列表爲空時應該返回0。 –

+0

好的!非常感謝。有效。 – user5460905