gpt4 book ai didi

android - 获取相同位置的数据并将其显示在 TextView 中

转载 作者:行者123 更新时间:2023-11-30 22:25:14 26 4
gpt4 key购买 nike

我实际上有 2 个问题。我正在做这个用户输入日志的安卓应用程序。在布局中,我列出了按地点分组的列表,我需要显示该地点的所有日志的数量。这是我对地点进行分组的代码

public ArrayList<Logs> getAllLogsGroupedByPlace() {
ArrayList<Logs> logByPlaceList = new ArrayList<Logs>();
String selectQuery = "SELECT * FROM " + TABLE_LOGS + " GROUP BY " + KEY_PLACE;

SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

if (cursor.moveToFirst()) {
do {
Logs log = new Logs();
log.setId(cursor.getLong(0));
log.setCreatedAt(cursor.getString(1));
log.setPlace(cursor.getString(2));
logByPlaceList.add(log);
}while (cursor.moveToNext());
}
return logByPlaceList;
}

这是我按位置计算日志的代码

public int getLogsCountPlace() {
String countQuery = "SELECT * FROM " + TABLE_LOGS + " WHERE " + KEY_PLACE + "=" + KEY_PLACE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();

return cursor.getCount();
}

所以我的问题是如何正确地计算这些按位置分组的日志,然后将它们显示在我的列表 TextView 中?

最佳答案

您应该更改 getLogsCountPlace 方法。

public Map<String,Integer> getLogsCountPlace() {
String countQuery = "SELECT COUNT(*), " + KEY_PLACE + " FROM " + TABLE_LOGS + " GROUP BY " + KEY_PLACE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
//// The cursor will return 2 columns, the first column will have the count of each place and the second column will have the place name. You could store the counts in a Map, having the place as the key and the count as the value.
cursor.close();
}

关于android - 获取相同位置的数据并将其显示在 TextView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35456281/

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