Join and display the results of two tables in onCreateLoader - Android Content Provider

195 views Asked by At

I want to create an activity called TableAandTableBActivity that will display the following information from my DB:

Select * from TableA, TABLEB where TableA._ID = TABLEB.productid

(Here _ID = product id)

I'm not sure how to do this. In my Content Provider class I created a static initialiser that joins tableA and TABLEB where TableA._ID = TABLEB.productid like this:

 static{
           sTableAWithTABLEB = new SQLiteQueryBuilder();

           //This is an inner join which looks like
           //TableA INNER JOIN TABLEB ON TableA.id = TABLEB.TableA_id
           sTableAWithTABLEB.setTables(
                   TableAContract.TableAEntry.TABLE_NAME + " INNER JOIN " +
                           TableAContract.TABLEBEntry.TABLE_NAME +
                           " ON " + TableAContract.TableAEntry.TABLE_NAME +
                           "." + TableAContract.TableAEntry._ID +
                           " = " + TableAContract.TABLEBEntry.TABLE_NAME +
                           "." + TableAContract.TABLEBEntry.COLUMN_TableA_ID);
       }

I started writing the code to display the results of my query in onCreateLoader in TableAandTableBActivity but now stuck. Please can someone show me how to solve this issue. Here are my code snippets:

public class TableAandTableBActivity extends AppCompatActivity implements LoaderManager.LoaderCallbacks {
   @Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_TABLEB);
   }

   @Override
   public Loader onCreateLoader(int id, Bundle args) {
       // Define a projection that specifies the columns from the table we care about.
       String[] projection = {
               TableAContract.TableAEntry._ID,
               TableAContract.TableAEntry.COLUMN_TableA_NAME,
               TableAContract.TableAEntry.COLUMN_TableA_QUANTITY,
               TableAContract.TableAEntry.COLUMN_TableA_CATEGORY,
               TableAContract.TableAEntry.COLUMN_TableA_PRICE};


       // Here is where I want to display the results of Select * from TableA, TABLEB where TableA._ID = TABLEB.productid
       return new CursorLoader(this,   // Parent activity context
               TableAContract.TableAEntry.CONTENT_URI,   // Provider content URI to query
               projection,             // Columns to include in the resulting Cursor
               null,                   // No selection clause
               null,                   // No selection arguments
               null);                  // Default sort order
   }

   @Override
   public void onLoadFinished(Loader loader, Object data) {

   }

   @Override
   public void onLoaderReset(Loader loader) {

   }
}



//

public class TableAProvider extends ContentProvider {

   private static final String LOG_TAG = TableAProvider.class.getSimpleName();
   static final int TableA = 100;
   private static final int TableA_ID = 101;
   static final int TABLEB = 200;
   private static final int TABLEB_ID = 201;
   static final int TableA_WITH_TABLEB = 300;
   private TableADbHelper mDbHelper;


   private static final SQLiteQueryBuilder sTableAWithTABLEB;


   private static final UriMatcher sUriMatcher = buildUriMatcher();

   static UriMatcher buildUriMatcher() {

       final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
       final String authority = TableAContract.CONTENT_AUTHORITY;

       matcher.addURI(authority, TableAContract.PATH_TableA, TableA);
       matcher.addURI(authority, TableAContract.PATH_TableA + "/#", TableA_ID);
       matcher.addURI(authority, TableAContract.PATH_TABLEB, TABLEB);
       matcher.addURI(authority, TableAContract.PATH_TABLEB + "/#", TABLEB_ID);
       matcher.addURI(authority, TableAContract.PATH_TableA + "/*", TableA_WITH_TABLEB);
       return matcher;
   }


   static{
       sTableAWithTABLEB = new SQLiteQueryBuilder();

       //This is an inner join which looks like
       //TableA INNER JOIN TABLEB ON TableA.id = TABLEB.TableA_id
       sTableAWithTABLEB.setTables(
               TableAContract.TableAEntry.TABLE_NAME + " INNER JOIN " +
                       TableAContract.TABLEBEntry.TABLE_NAME +
                       " ON " + TableAContract.TableAEntry.TABLE_NAME +
                       "." + TableAContract.TableAEntry._ID +
                       " = " + TableAContract.TABLEBEntry.TABLE_NAME +
                       "." + TableAContract.TABLEBEntry.COLUMN_TableA_ID);
   }



//

public final class TableAContract {

   private TableAContract() {
   }

   public static final String CONTENT_AUTHORITY = "com.test.androidtwotaabl.es";
   public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

   public static final String PATH_TableA = "TableA";
   public static final String PATH_TABLEB = "TABLEB";

   public static final class TableAEntry implements BaseColumns {

       public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_TableA);
       public static final String CONTENT_LIST_TYPE =
               ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TableA;
       public static final String CONTENT_ITEM_TYPE =
               ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TableA;

       public final static String TABLE_NAME = "TableA";
       public final static String _ID = BaseColumns._ID;
       public final static String COLUMN_TableA_NAME = "TableAName";
       public final static String COLUMN_TableA_QUANTITY = "TableAQuantity";
       public final static String COLUMN_TableA_PRICE = "TableAPrice";
       public final static String COLUMN_TableA_CATEGORY = "TableACategory";
       public final static String COLUMN_TableA_OLDPRICE = "oldPrice";

       public static Uri TableAWithTABLEB() {
           return CONTENT_URI.buildUpon().appendPath(PATH_TABLEB).build();
       }

   }

   public static final class TABLEBEntry implements BaseColumns {
       public static final Uri CONTENT_URI = Uri.withAppendedPath(BASE_CONTENT_URI, PATH_TABLEB);
       public static final String CONTENT_LIST_TYPE =
               ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TABLEB;
       public static final String CONTENT_ITEM_TYPE =
               ContentResolver.CURSOR_ITEM_BASE_TYPE + "/" + CONTENT_AUTHORITY + "/" + PATH_TABLEB;

       public final static String TABLE_NAME = "TABLEB";
       public final static String _ID = BaseColumns._ID;
       public final static String COLUMN_TableA_ID = "TableAId";
   }

}
0

There are 0 answers