gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:17:03 25 4
gpt4 key购买 nike

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

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

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

最佳答案

我不知道如何在每个文件中做到这一点。

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;
}

}

关于android - 使用 GROUP BY 从 SQLiteDatabase 获取行,然后使用 SimpleCursorAdapter 呈现它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2764248/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com