Android Content provider Cursor返回0

时间:2013-08-10 16:31:28

标签: android android-contentprovider android-sqlite

我已经看了几天了,我完全无法理解为什么我的内容提供商使用我传递的参数返回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;
}

我打印了日志中的内容,我看到了_id = 3&#39;这应该没问题,因为我已经删除了我的SQLite数据库的副本,我可以看到支出表中有一个_id 3行。有任何想法吗?

1 个答案:

答案 0 :(得分:0)

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

selectionClause = BusinessOpsExpenditureProvider.EXPENDITURE_ID + "=?";

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

selectionClause = BusinessOpsDatabase.EXPENDITURE_ID + "=?";

并且有效!