我有一个包含多列的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;
}
}
这就是我填充arraylists的方式。包含子项的arraylist嵌套两次:最里面包含3个要显示在同一行上的对象,“middle”将最里面的arraylists组合在一起,以便它们出现在可扩展列表视图的正确分组下。
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);
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
对于使用sqlite数据库的可扩展listview:您可以使用CursorTreeAdapter而不是使用BaseExpandablelistAdapter。这将很容易让您完成任务。 请参考以下链接:
http://developer.android.com/reference/android/widget/CursorTreeAdapter.html
答案 2 :(得分:0)
public View getChildView(int parentPosition, int childPosition, boolean isLastChild, View convertView,
ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_view_list, null);
}
final String abc = (String) getChild(parentPosition, childPosition);
TextView status_tv = (TextView) convertView
.findViewById(R.id.child_tv);
status_tv.setText(abc);
//ImageView imgListChild=(ImageView) convertView.findViewById(R.id.child_iv);
//imgListChild.set
return convertView;
}