0

以下是我尝试获取应用程序名称 (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;
    }

主要活动

和上面一样。。。

错误

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
4

1 回答 1

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.

于 2013-02-19T17:47:47.050 回答