简体   繁体   English

Android ListView-从SQLite获取值,其中一列为分组依据

[英]Android ListView - Get Values from SQLite with group by for one of the columns

I am rendering columns from SQLite table as ListView in my android application. 我在我的Android应用程序中将SQLite表中的列呈现为ListView。 The data looks like this: 数据如下所示:

Project Name   OutletName  param
RA_Pepsi       Tesco       Shelfstrip left
RA_Pepsi       Tesco       Shelfstrip right
RA_Cocacola    Tesco       Shelfstrip top
RA_Cocacola    Coors       Shelfstrip bottom

I am using the following code to render the ListView 我正在使用以下代码来呈现ListView

sqLiteDatabase = sqLiteHelper.getWritableDatabase();
cursor = sqLiteDatabase.rawQuery("SELECT * FROM "+SQLiteHelper.TABLE_NAME2+"", null);
ProjectName_Array.clear();
OutletName_Array.clear();
ParamName_Array.clear();


if (cursor != null && cursor.getCount() > 0) {
    if (cursor.moveToFirst()) {
        do {
                    ProjectName_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table2_Column_ProjectName)));
                    OutletName_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table2_Column_OutletName)));
                    ParamName_Array.add(cursor.getString(cursor.getColumnIndex(SQLiteHelper.Table2_Column_PARAM)));
                } while (cursor.moveToNext());
            }
        } else {
            Toast.makeText( this, "No data to display!", Toast.LENGTH_SHORT ).show();
        }

With a custom adapter, this renders the data as ListView as follows: 使用自定义适配器,这将数据呈现为ListView,如下所示:

RA_Pepsi
Tesco
Shelfstrip left

RA_Pepsi
Tesco
Shelfstrip right

RA_CocaCola
Tesco
Shelfstrip top

RA_CocaCola
Coors
Shelfstrip bottom

However, I want to aggregate the param at each project and store level and display as follows 但是,我想在每个项目和存储级别汇总参数,并显示如下

RA_Pepsi
Tesco
Shelfstrip left
Shelfstrip right

RA_CocaCola
Tesco
Shelfstrip top

When I tried the group by option using both projects and outlet name columns - I get only one of the parameter rendered. 当我同时使用项目和插座名称列尝试按组分组时,我仅获得一个呈现的参数。 How can display the list view with aggregated data. 如何显示带有聚合数据的列表视图。

Try use SQLite's group_concat() function,take a look here . 尝试使用SQLite的group_concat()函数, 在此处查看

Change your sql like: 像这样更改您的sql:

SELECT 'Project Name',OutletName,group_concat(param) as params
FROM tableName
GROUP BY 'Project Name',OutletName

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM