2013-02-19 31 views
0

繼Android的SQLite數據庫數列的詳細信息列表視圖顯示計數是我嘗試讓應用程序的名稱(KEY_NAME),application_category(KEY_CATEGORY)和application_permissions細節的代碼(KEY_PERM )如何一行具有

但我真正想要的是與應用程序的名稱和類別,以顯示權限(計數)我如何能做到的數量,我在列表視圖

處理

public Cursor queueAll() { 
     String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_CATEGORY, 
       "count" + KEY_PERM }; 
     Cursor cursor = sqlitedb.query(MYDATABASE_TABLE, columns, null, null, 
       KEY_NAME, null, null); 
     return cursor; 
    } 
顯示它

主要活動

mySQLiteAdapter = new Handle(this); 
     mySQLiteAdapter.openToWrite(); 
     cursor = mySQLiteAdapter.queueAll(); 
     String[] from = new String[] { Handle.KEY_NAME, Handle.KEY_PERM, 
       Handle.KEY_CATEGORY }; 
     int[] to = new int[] { R.id.appname, R.id.numbpermcount, R.id.category }; 
     cursorAdapter = new SimpleCursorAdapter(this, 
       R.layout.activity_main_row, cursor, from, to); 
     listContent.setAdapter(cursorAdapter); 

謝謝你提前。

我試着再次這樣做。

處理器

public static final String KEY_ID = "_id"; 
public static final String KEY_NAME = "application_name"; 
public static final String KEY_PACK = "application_package"; 
public static final String KEY_PERM = "application_permission"; 
public static final String KEY_LEVEL = "application_level"; 
public static final String KEY_CATEGORY = "application_category"; 

public Cursor queueAll() { 
     String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_CATEGORY, 
       "count(" + KEY_PERM +")" }; 
     Cursor cursor = sqlitedb.query(MYDATABASE_TABLE, columns, null, null, 
       KEY_NAME, null, null); 
     return cursor; 
    } 

主要活動

是同上...

ERROR

02-19 22:51:04.049: E/AndroidRuntime(1192): java.lang.RuntimeException: Unable to start activity ComponentInfo{db.database/db.database.MainActivity}: java.lang.IllegalArgumentException: column 'application_permission' does not exist 
+0

不會計數(許可)必須是子查詢嗎? – Sam 2013-02-19 16:57:56

+0

我認爲你的列需要像這樣:String [] columns = new String [] {KEY_ID,KEY_NAME,KEY_CATEGORY, 「count(」+ KEY_PERM +「)」}; – 2013-02-19 16:59:17

回答

1

我認爲你正在試圖做的,什麼是超出了內建的query方法所能做的。相反,你需要rawquery,你可以傳遞任意SQL。這聽起來像你需要

public static final String KEY_ID = "_id"; 
public static final String KEY_NAME = "application_name"; 
public static final String KEY_PACK = "application_package"; 
public static final String KEY_PERM = "application_permission"; 
public static final String KEY_LEVEL = "application_level"; 
public static final String KEY_CATEGORY = "application_category"; 

public static final String KEY_PERM_COUNT = "application_permission_COUNT"; 

public Cursor queueAll() { 
    Cursor cursor = sqlitedb.rawquery(
     "SELECT "+KEY_ID+", "+KEY_NAME+", "+KEY_CATEGORY 
     +", count("+KEY_PERM+") AS "+KEY_PERM_COUNT 
     +" FROM "+MYDATABASE_TABLE 
     +" GROUP BY "+KEY_ID+", "+KEY_NAME+", "+KEY_CATEGORY 
    ); 
    return cursor; 
} 

然後設置您的適配器也讀Handle.KEY_PERM_COUNT

+0

謝謝你隊友...它工作正常。非常感謝你教我關於原始查詢。 :) – Romi 2013-02-20 00:45:36

相關問題