在Android中使用光标的表联合

时间:2010-10-16 13:19:32

标签: android sqlite cursor union

我正在尝试UNION两个具有相同字段的表来创建我用来创建ListView的单个游标(通过内容提供程序)。

@Override
 public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {

  SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
  String groupBy = null;

  switch (sUriMatcher.match(uri)) {

  case LIST:
   StringBuilder sb = new StringBuilder();

   for (String s : projection)
    sb.append(s).append(",");

   String projectionStr = sb.toString();
   projectionStr = projectionStr.substring(0,
     projectionStr.length() - 1);

   String[] subQueries = new String[] {
     "SELECT " + projectionStr + " FROM " + Customer.TABLE_NAME,
     "SELECT " + projectionStr + " FROM "
       + IndividualCustomer.TABLE_NAME };
   String sql = qb.buildUnionQuery(subQueries, sortOrder, null);
   SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
   Cursor mCursor = db.rawQuery(sql, null);

   mCursor.setNotificationUri(getContext().getContentResolver(), uri);

   return mCursor;

即使两个表都是空的,我也会得到两个空行,这会在listview中创建两行。我如何摆脱这个问题?

此外,当我从ListView中删除一行时,尽管有setNotificationUri()

,但游标仍未更新

任何指针,都将非常感激

1 个答案:

答案 0 :(得分:1)

已解决 - 我必须提供group by子句,因为其中一个(投影的)列具有“TOTAL(...)”功能。