使用 GROUP BY 从 SQLiteDatabase 获取行,然后使用 SimpleCursorAdapter 呈现它们

发布于 2024-08-31 09:27:23 字数 294 浏览 4 评论 0原文

我想显示每天在一个项目上花费的总秒数。

我想使用 SQLiteDatabase 使用下面的查询来获取行,然后使用 SimpleCursorAdapter 填充 ListView。

SELECT projectId, date, sum(endedAt-startedAt) as hours
FROM project_reports
WHERE startedAt >= 1270098000
AND endedAt <= 1272776399
GROUP BY date, projectId

I want to display total amount of seconds that was spent each day on a project.

I want to use SQLiteDatabase to fetch rows using the query below and then use SimpleCursorAdapter to fill a ListView.

SELECT projectId, date, sum(endedAt-startedAt) as hours
FROM project_reports
WHERE startedAt >= 1270098000
AND endedAt <= 1272776399
GROUP BY date, projectId

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回忆追雨的时光 2024-09-07 09:27:24

我无法弄清楚如何在每个文件中执行此操作。

ItemsDbAdapter:

    public Cursor findAllGrouped(Date from, Date to) {
    String where = KEY_STARTEDAT + " >= " + from.getTime() + " AND " + KEY_STARTEDAT + " <= "
            + to.getTime();

    String[] columns = new String[] { KEY_ROWID, KEY_PROJECTID, KEY_DATE,
            "sum(" + KEY_ENDEDAT + "-" + KEY_STARTEDAT + ") as hours" };

    return _dbHelper.getDb().query(true, DATABASE_TABLE, columns, where, null,
            KEY_DATE + ", " + KEY_PROJECTID, null, null, null);

}

活动:

    private void setWeek(int weekNumber) {

    Date from, to;
    Calendar calendar = new GregorianCalendar();
    calendar.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);
    calendar.set(GregorianCalendar.WEEK_OF_YEAR, weekNumber); 
    from = calendar.getTime();
    calendar.add(GregorianCalendar.DAY_OF_WEEK, 6);
    to = calendar.getTime();

    this._itemsCursor = _itemsAdapter.findAllGrouped(from, to);
    startManagingCursor(_itemsCursor);
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            R.layout.submit_list_row, _itemsCursor,
            new String[] { ItemsDbAdapter.KEY_PROJECTID, ItemsDbAdapter.KEY_DATE, "hours" },
            new int[] {R.id.date, R.id.title, R.id.hours});
    cursorAdapter.setViewBinder(new MyViewBinder());

    ListView view = (ListView)findViewById(R.id.items);
    view.setAdapter(cursorAdapter);
    view.setOnItemClickListener(this);
    Log.i("List", "Displaying week " + weekNumber);
}

// since we want to use the same field twice (once for date and once for time)
class MyViewBinder implements SimpleCursorAdapter.ViewBinder
{

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        TextView textField;
        view = (View)view.getParent();

        switch (columnIndex) {
            case 1: //projectid
                textField = (TextView)view.findViewById(R.id.title);
                textField.setText(_projectsAdapter.getTitle(cursor.getInt(columnIndex)));
                cursor.getColumnName(1);
                return true;
            case 2: //date
                Time time = new Time();
                time.set(cursor.getLong(columnIndex));
                String tertxc = android.text.format.DateUtils.getDayOfWeekString(time.weekDay, DateUtils.LENGTH_MEDIUM);
                textField = (TextView)view.findViewById(R.id.date);
                textField.setText(tertxc);

                return true;
            case 3: //hours
                textField = (TextView)view.findViewById(R.id.hours);
                int hours = cursor.getInt(columnIndex) / 3600000; //3600000 milliseconds
                textField.setText(hours + "");
                return true;
        }

        return true;
    }

}

I couldn't figure out how to do it in each file.

ItemsDbAdapter:

    public Cursor findAllGrouped(Date from, Date to) {
    String where = KEY_STARTEDAT + " >= " + from.getTime() + " AND " + KEY_STARTEDAT + " <= "
            + to.getTime();

    String[] columns = new String[] { KEY_ROWID, KEY_PROJECTID, KEY_DATE,
            "sum(" + KEY_ENDEDAT + "-" + KEY_STARTEDAT + ") as hours" };

    return _dbHelper.getDb().query(true, DATABASE_TABLE, columns, where, null,
            KEY_DATE + ", " + KEY_PROJECTID, null, null, null);

}

Activity:

    private void setWeek(int weekNumber) {

    Date from, to;
    Calendar calendar = new GregorianCalendar();
    calendar.set(GregorianCalendar.DAY_OF_WEEK, GregorianCalendar.MONDAY);
    calendar.set(GregorianCalendar.WEEK_OF_YEAR, weekNumber); 
    from = calendar.getTime();
    calendar.add(GregorianCalendar.DAY_OF_WEEK, 6);
    to = calendar.getTime();

    this._itemsCursor = _itemsAdapter.findAllGrouped(from, to);
    startManagingCursor(_itemsCursor);
    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,
            R.layout.submit_list_row, _itemsCursor,
            new String[] { ItemsDbAdapter.KEY_PROJECTID, ItemsDbAdapter.KEY_DATE, "hours" },
            new int[] {R.id.date, R.id.title, R.id.hours});
    cursorAdapter.setViewBinder(new MyViewBinder());

    ListView view = (ListView)findViewById(R.id.items);
    view.setAdapter(cursorAdapter);
    view.setOnItemClickListener(this);
    Log.i("List", "Displaying week " + weekNumber);
}

// since we want to use the same field twice (once for date and once for time)
class MyViewBinder implements SimpleCursorAdapter.ViewBinder
{

    @Override
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        TextView textField;
        view = (View)view.getParent();

        switch (columnIndex) {
            case 1: //projectid
                textField = (TextView)view.findViewById(R.id.title);
                textField.setText(_projectsAdapter.getTitle(cursor.getInt(columnIndex)));
                cursor.getColumnName(1);
                return true;
            case 2: //date
                Time time = new Time();
                time.set(cursor.getLong(columnIndex));
                String tertxc = android.text.format.DateUtils.getDayOfWeekString(time.weekDay, DateUtils.LENGTH_MEDIUM);
                textField = (TextView)view.findViewById(R.id.date);
                textField.setText(tertxc);

                return true;
            case 3: //hours
                textField = (TextView)view.findViewById(R.id.hours);
                int hours = cursor.getInt(columnIndex) / 3600000; //3600000 milliseconds
                textField.setText(hours + "");
                return true;
        }

        return true;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文