1

我已经看了几天了,我完全无法弄清楚为什么我的内容提供者使用我传递的参数返回 0。

这是我的 contentResolver 代码:

String[] expenditureProjection = {
            BusinessOpsDatabase.COL_EXPEND_CAT_ID,
            BusinessOpsDatabase.COL_EXPEND_DATE,
            BusinessOpsDatabase.COL_EXPEND_AMOUNT,
            BusinessOpsDatabase.COL_EXPEND_DESC,
            BusinessOpsDatabase.COL_STERLING_EXCHANGE,
            BusinessOpsDatabase.COL_COMPANY_ID,
            BusinessOpsDatabase.CURRENCY_ID,
            BusinessOpsDatabase.COL_MOD_DATE
    };
    // Defines a string to contain the selection clause
    String selectionClause = null;

    // An array to contain selection arguments
    String[] selectionArgs = {expend_id.trim()};

    selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";

    Log.d(TAG, expend_id+" Selected from list.");

    Cursor expendCursor = getContentResolver().query(
            BusinessOpsExpenditureProvider.CONTENT_URI, expenditureProjection, selectionClause, selectionArgs, null);
    if (null == expendCursor) {
        Log.d(TAG, "Expenditure cursor: Is null");
    } else if (expendCursor.getCount() < 1) {
        Log.d(TAG,"Expenditure cursor: Search was unsuccessful: "+expendCursor.getCount());
    } else {
        Log.d(TAG,"Expenditure cursor: Contains results");

        int i=0;
        expendCursor.moveToFirst();
        // loop through cursor and populate country array
        while (expendCursor.isAfterLast() == false)
        {
            expend_date_edit.setText(expendCursor.getString(1));
            expend_amount_edit.setText(expendCursor.getString(3));
            expend_desc_edit.setText(expendCursor.getString(4));

            i++;
            expendCursor.moveToNext();
        }
    }

这是我的内容提供者查询方法:

@Override
public Cursor query(Uri uri, String[] projection, String selection,
                    String[] selectionArgs, String sortOrder) {
    SQLiteDatabase db = mDB.getWritableDatabase();

    // A convenience class to help build the query
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

    qb.setTables(BusinessOpsDatabase.TABLE_EXPENDITURE);

    switch (sURIMatcher.match(uri)) {
        case EXPENDITURE:
            if(selection != null && selectionArgs != null){
                //values.get("company_contact");
            String segment = uri.getLastPathSegment();
            Log.d(TAG, "Last path segment: "+ segment);
            String whereClause = BusinessOpsDatabase.EXPENDITURE_ID + "="+ selectionArgs[0];
            Log.d(TAG, "Where clause: "+whereClause);
            }
            break;
        case EXPENDITURE_ID:
            // If this is a request for an individual status, limit the result set to that ID
            qb.appendWhere(BusinessOpsDatabase.EXPENDITURE_ID + "=" + uri.getLastPathSegment());
            break;
        default:
            throw new IllegalArgumentException("Unsupported URI: " + uri);
    }


    // Query the underlying database
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, null);

    // Notify the context's ContentResolver if the cursor result set changes
    c.setNotificationUri(getContext().getContentResolver(), uri);

    // Return the cursor to the result set
    return c;
}

我正在将 whereclause 打印到日志中,我看到 '_id=3' 这应该没问题,因为我已经提取了我的 SQLite 数据库的副本,并且我可以看到支出表中有一个 _id 3 行。有任何想法吗?

4

1 回答 1

0

这是一个多么史诗般的问题。我在 ContentResolver 代码中发现了错误。

selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";

我使用的是提供者而不是数据库类的 EXPENDITURE_ID 变量。该行现在读取。

selectionClause = BusinessOpsDatabase.EXPENDITURE_ID + "=?";

并且有效!

于 2013-08-12T15:46:42.757 回答