我试图制作一个可扩展的列表视图,其中我的标题来自我的数据库,而我的子项是静态的,在每个标题中重复显示它。
在我的情况下,我已经完成了标题部分,但每当我单击列表视图中的项目以显示应用程序崩溃的子项时。
我总是有这个错误。
Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference
这是我的ExpandableListAdapter
public class ExpandListAdapter extends BaseExpandableListAdapter {
private Context _context;
List<String> child;
private ArrayList<Details> mDetails;
private HashMap<ArrayList<Details>, List<String>> mchildOfDetails;
public ExpandListAdapter(Context context, HashMap<ArrayList<Details>, List<String>> listChildData, ArrayList<Details> details) {
this._context = context;
this.mDetails = details;
this.mchildOfDetails = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.mchildOfDetails.get(mDetails.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 infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandlist_child_item, parent,false);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.child_info);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this.mchildOfDetails.size();
}
@Override
public Object getGroup(int groupPosition) {
return this.mDetails.get(groupPosition);
}
@Override
public int getGroupCount() {
return mDetails.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.expandlist_group_item, null);
}
TextView lblDesc = (TextView) convertView.findViewById(R.id.tv_desc);
Details mDetailsobj = mDetails.get(groupPosition);
lblListDesc.setText(mDetailsobj.getDesc());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这是我使用expandable listview
的片段ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> child = new ArrayList<String>();
HashMap<ArrayList<Details>, List<String>> listDataChild;
ArrayList<Details> Details;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
....
expListView = (ExpandableListView) v.findViewById(R.id.lvExp);
prepareChildData();
listDataChild = new HashMap<ArrayList<Details>, List<String>>();
listDataChild.put(Details, child);
listAdapter = new ExpandListAdapter(getActivity(), listDataChild, Details);
expListView.setAdapter(listAdapter);
....
}
private void prepareChildData(){
child = new ArrayList<String>();
child.add("One");
child.add("Two");
child.add("Three");
}
另一个说明:
我想我的getChild()中有错误。因为我无法在getChildView()中显示正在显示的子列表。
提前感谢您的帮助。
答案 0 :(得分:0)
所以,我所做的是创建一个新的List来处理子项目
//header titles
private ArrayList<DetailsWithNoLocation> mDetailsWithNoLocation;
//child titles
List<String> mlistOtherDetails;
// child data in format of header title, child title
private HashMap<ArrayList<Details>, List<String>> mchildOfDetails;
public ExpandListAdapter(Context context, ArrayList<Details> details, List<String> listOtherDetails, HashMap<ArrayList<Details>, List<String>> HashChildData) {
this._context = context;
this.mDetails = detailsWithNoLocation; //header
this.mlistOtherDetails = listOtherDetails; // child
this.mchildOfDetails = HashChildData; //hash data
}
然后在我的getChild()中,而不是返回上次调用新创建的List时使用的内容
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this.mlistOtherDetails.get(groupPosition);
}
我现在可以显示我的子视图,但不是在每行中显示三个值{One,Two,Three},而是单独显示它们。在每一行显示一个值,所以我仍在使用它。