我有一个包含多列的 SQLite 数据库,其中一列是日期。我想将数据库中的数据填充到可扩展的列表视图中,以日期为标题。因此,对于共享相同日期值的所有行,我想将它们显示为该特定日期的子项。
我使用arraylist来存储数据库中的数据。我显示了组标题,但是当我尝试展开时出现空指针异常。该错误来自 getChildView 方法。
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
public Object getChild(int groupPosition, int childPosition) {
return infoList.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
RelativeLayout infoLayout = (RelativeLayout) findViewById(R.id.infoLayout);
String exercise = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(0).toString();
String weight = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(1).toString();
String reps = ((ArrayList<Object>) getChild(groupPosition, childPosition)).get(2).toString();
((TextView) infoLayout.findViewById(R.id.exercise)).setText(exercise);
((TextView) infoLayout.findViewById(R.id.weight)).setText(weight);
((TextView) infoLayout.findViewById(R.id.reps)).setText(reps);
return infoLayout;
}
public int getChildrenCount(int groupPosition) {
return infoList.get(groupPosition).size();
}
public Object getGroup(int groupPosition) {
return dateList.get(groupPosition);
}
public int getGroupCount() {
return dateList.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
TextView dateView = new TextView(getApplicationContext());
dateView.setText(getGroup(groupPosition).toString());
return dateView;
}
public boolean hasStableIds() {
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
这就是我填充数组列表的方式。包含子项的数组列表嵌套了两次:最里面的包含 3 个要在同一行上显示的对象,“中间”一个将最里面的数组列表组合在一起,以便它们出现在可扩展列表视图的正确分组下。
private DatabaseMethods dbm;
private ArrayList<String> dateList = new ArrayList<String>();
private ArrayList<ArrayList<ArrayList<Object>>> infoList = new ArrayList<ArrayList<ArrayList<Object>>>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stats);
dbm = new DatabaseMethods(this);
dbm.read();
Cursor dates = dbm.fetchGroup();
int DATES_INDEX = dates.getColumnIndexOrThrow("date");
while (dates.moveToNext()) {
String date = dates.getString(DATES_INDEX);
if (!dateList.contains(date)) {
dateList.add(date);
}
}
for (String date : dateList) {
Cursor info = dbm.fetchChildren(date);
int EXERCISE_INDEX = info.getColumnIndexOrThrow("exercise");
int WEIGHT_INDEX = info.getColumnIndexOrThrow("weight");
int REPS_INDEX = info.getColumnIndexOrThrow("reps");
ArrayList<ArrayList<Object>> secondInnerInfoList = new ArrayList<ArrayList<Object>>();
while (info.moveToNext()) {
String exercise = info.getString(EXERCISE_INDEX);
int weight = info.getInt(WEIGHT_INDEX);
int reps = info.getInt(REPS_INDEX);
ArrayList<Object> thirdInnerInfoList = new ArrayList<Object>();
thirdInnerInfoList.add(exercise);
thirdInnerInfoList.add(weight);
thirdInnerInfoList.add(reps);
secondInnerInfoList.add(thirdInnerInfoList);
infoList.add(secondInnerInfoList);
}
}