如何在android sqlite数据库中的列列表详细信息的行列表视图中显示计数

时间:2013-02-19 16:48:41

标签: 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

1 个答案:

答案 0 :(得分: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