views:

26

answers:

1

Hi,

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

A: 

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

Sameer Segal