簡體   English   中英

Android ListView-從SQLite獲取值,其中一列為分組依據

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

我在我的Android應用程序中將SQLite表中的列呈現為ListView。 數據如下所示:

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

我正在使用以下代碼來呈現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();
        }

使用自定義適配器,這將數據呈現為ListView,如下所示:

RA_Pepsi
Tesco
Shelfstrip left

RA_Pepsi
Tesco
Shelfstrip right

RA_CocaCola
Tesco
Shelfstrip top

RA_CocaCola
Coors
Shelfstrip bottom

但是,我想在每個項目和存儲級別匯總參數,並顯示如下

RA_Pepsi
Tesco
Shelfstrip left
Shelfstrip right

RA_CocaCola
Tesco
Shelfstrip top

當我同時使用項目和插座名稱列嘗試按組分組時,我僅獲得一個呈現的參數。 如何顯示帶有聚合數據的列表視圖。

嘗試使用SQLite的group_concat()函數, 在此處查看

像這樣更改您的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