我的自定义内容提供程序返回空游标

时间:2013-07-25 10:37:47

标签: android android-contentprovider android-sqlite

我有一个非常奇怪的问题,下面的查询返回一个游标,但是计数是0虽然数据在db中已经检查过,

Cursor cursor = context.getContentResolver().query(VueConstants.CONTENT_URI,
    new String[] {"aisleId"}, null, null, null);
Cursor cursor = context.getContentResolver().query(VueConstants.CONTENT_URI,
    null, null, null, null);                                                                              

以下是我使用uri matcher在我的客户内容提供商中覆盖的ContentProvider类的查询方法

private static final UriMatcher URIMATCHER;

/** uri matchers for articles table and articles fts3 table query method. */
static {
URIMATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URIMATCHER.addURI(VueConstants.AUTHORITY, VueConstants.AISLES,
    AISLES_MATCH);
URIMATCHER.addURI(VueConstants.AUTHORITY, VueConstants.AISLES
    + "/#", AISLE_MATCH);
}

@Override
public Cursor query(Uri uri, String[] projection, String selection,
  String[] selectionArgs, String sortOrder) {
SQLiteDatabase aislesDB = dbHelper.getWritableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
Cursor cursor = null;
String id;

switch (URIMATCHER.match(uri)) {
  case AISLES_MATCH:
    qb.setTables(VueConstants.AISLES);
    cursor = qb.query(aislesDB, projection, selection, selectionArgs,
        null, null, sortOrder);
    Log.e("provider", "Total Aisles marked to Content Provider cursor.getCount()" + cursor.getCount());
    break;
 case AISLE_MATCH:
   qb.setTables(VueConstants.AISLES);
   id = uri.getLastPathSegment();
   cursor = qb.query(aislesDB, projection,
       VueConstants.AISLE_ID+ "=" + id
           + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
               : ""), selectionArgs, null, null, null);
    break;
 case AISLE_IMAGES_MATCH:
   qb.setTables(VueConstants.AISLE_IMAGES);
   cursor = qb.query(aislesDB, projection, selection, selectionArgs,
       null, null, sortOrder);
   break;
 case IMAGE_MATCH:
   qb.setTables(VueConstants.AISLE_IMAGES);
   id = uri.getLastPathSegment();
   cursor = qb.query(aislesDB, projection,
       VueConstants.IMAGE_ID+ "=" + id
           + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
               : ""), selectionArgs, null, null, null);
   break;
 case LOOKING_FOR_TABLE_MATCH:
   qb.setTables(VueConstants.LOOKING_FOR_TABLE);
   cursor = qb.query(aislesDB, projection, selection, selectionArgs,
       null, null, sortOrder);
   break;
 case LOOKING_FOR_ROW_MATCH:
   qb.setTables(VueConstants.LOOKING_FOR_TABLE);
   id = uri.getLastPathSegment();
   cursor = qb.query(aislesDB, projection,
       VueConstants.ID+ "=" + id
           + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
               : ""), selectionArgs, null, null, null);
   break;
 case OCCATION_TABLE_MATCH:
   qb.setTables(VueConstants.OCCASION_TABLE);
   cursor = qb.query(aislesDB, projection, selection, selectionArgs,
       null, null, sortOrder);
   break;
 case OCCATION_ROW_MATCH:
   qb.setTables(VueConstants.OCCASION_TABLE);
   id = uri.getLastPathSegment();
   cursor = qb.query(aislesDB, projection,
       VueConstants.ID+ "=" + id
           + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
               : ""), selectionArgs, null, null, null);
 break;
 case CATEGORY_TABLE_MATCH:
   qb.setTables(VueConstants.CATEGORY_TABLE);
   cursor = qb.query(aislesDB, projection, selection, selectionArgs,
       null, null, sortOrder);
   break;
 case CATEGORY_ROW_MATCH:
   qb.setTables(VueConstants.CATEGORY_TABLE);
   id = uri.getLastPathSegment();
   cursor = qb.query(aislesDB, projection,
       VueConstants.ID+ "=" + id
           + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
               : ""), selectionArgs, null, null, null);
   break;
 case COMMENTS_TABLE_MATCH:
   qb.setTables(VueConstants.COMMENTS_ON_IMAGES_TABLE);
   cursor = qb.query(aislesDB, projection, selection, selectionArgs,
       null, null, sortOrder);
   break;
 case COMMENTS_ROW_MATCH:
   qb.setTables(VueConstants.COMMENTS_ON_IMAGES_TABLE);
   id = uri.getLastPathSegment();
   cursor = qb.query(aislesDB, projection,
       VueConstants.ID+ "=" + id
           + (!TextUtils.isEmpty(selection) ? " AND (" + selection + ')'
               : ""), selectionArgs, null, null, null);
   break;

}
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;

}

1 个答案:

答案 0 :(得分:2)

VueConstants.CONTENT_URI的价值是多少? 你有没有尝试过Uri:

"content://"+VueConstants.AUTHORITY+"/"+ VueConstants.AISLES+"/1"

"content://"+VueConstants.AUTHORITY+"/"+ VueConstants.AISLES