Union of Tables with Cursor in Android

淺唱寂寞╮ 提交于 2019-12-11 03:38:54

问题


I am trying to UNION two tables with the same fields to create a single cursor (through a content provider) that I am using to create my 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;

Even if the two tables are empty, I get two null rows, which creates two rows in my listview. How do I get rid of this problem?

Additionally, when I delete a row from the ListView, the cursor is not getting updated in spite of setNotificationUri()

Any pointers, will be most appreciated


回答1:


Solved - I had to supply a group by clause as one of the columns (of the projection) had a "TOTAL(...)" function.



来源:https://stackoverflow.com/questions/3949042/union-of-tables-with-cursor-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!