-1

我有一个包含多列的 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);
        }

    }
4

3 回答 3

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;
}
于 2013-11-12T09:32:06.803 回答
0
  1. 编写一个方法来获取数据并将其转换为 ArrayList 或数组字符串。
  2. 在您的 Activity 中为 ArrayAdapter 声明和初始化数据,例如: ArrayAdapter mAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, yourArrayListOrArray);
  3. ListView.setAdapter(mAdapter);
于 2012-12-14T03:10:19.130 回答
0

对于带有 sqlite 数据库的可扩展列表视图:您可以使用 CursorTreeAdapter,而不是使用 BaseExpandablelistAdapter。这将使您轻松完成任务。请参考以下链接:

http://developer.android.com/reference/android/widget/CursorTreeAdapter.html

于 2013-11-11T11:20:32.417 回答