使用 ExpandableListView 处理空组点击
Handling empty group clicks with ExpandableListView
我正在使用 ExpandableListView 创建导航抽屉,但我不知道如何处理空组点击。
每次我尝试点击一个空组时,我都会收到一个 NullPointerException,上面写着 "Attempt to invoke interface method 'int java.util.List.size()' on a null object reference" 并且它指向 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;
}
}
如有任何帮助,我们将不胜感激。
您调用 size()
的列表为空,因此您必须先检查一下。
试试这个:
@Override
public int getChildrenCount(int groupPosition) {
List childList = listDataChild.get(listDataItem.get(groupPosition));
if (childList != null && ! childList.isEmpty()) {
return childList.size();
}
return 1;
}
我正在使用 ExpandableListView 创建导航抽屉,但我不知道如何处理空组点击。
每次我尝试点击一个空组时,我都会收到一个 NullPointerException,上面写着 "Attempt to invoke interface method 'int java.util.List.size()' on a null object reference" 并且它指向 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;
}
}
如有任何帮助,我们将不胜感激。
您调用 size()
的列表为空,因此您必须先检查一下。
试试这个:
@Override
public int getChildrenCount(int groupPosition) {
List childList = listDataChild.get(listDataItem.get(groupPosition));
if (childList != null && ! childList.isEmpty()) {
return childList.size();
}
return 1;
}