2012-12-28 129 views
9

我有兩個1:1關係的表,我使用內容提供者和光標加載器。CursorLoader - 兩個表連接查詢?

我如何使用加載器進行連接查詢?我可以用內部提供程序中的rawSql以某種方式破解它,但是如何在遊標加載器構造函數中完成它是超出我的。

非常感謝!

private static final String CREATE_TABLE_ARTICLES = "create table " 
      + TABLE_ARTICLES + "(" 
      + COLUMN_ARTICLE_ID + " integer primary key autoincrement, " 
      + COLUMN_URL + " text not null unique, " 
      + COLUMN_TITLE + " text not null, " 
      + COLUMN_PRICE + " text not null, " 
      + COLUMN_ADDED + " text not null, " 
      + COLUMN_IMG_URL + " text);"; 

    private static final String CREATE_TABLE_ARTICLE_DETAIL = "create table " 
      + TABLE_ARTICLE_DETAILS + "(" 
      + COLUMN_ARTICLE_DETAIL_ID + " integer primary key autoincrement, " 
      + COLUMN_DESC + " text not null, " 
      + COLUMN_LOCALITY + " text, " 
      + COLUMN_TYPE + " text not null, " 
      + COLUMN_SELLER + " text not null, " 
      + COLUMN_SELLER_PHONE + " text, " 
      + COLUMN_IMAGE_COUNT + " integer default 0, " 
      + COLUMN_ARTICLE + " integer, foreign key (" + COLUMN_ARTICLE + ") references " + TABLE_ARTICLES + "(" + COLUMN_ARTICLE_ID + "));"; 
+0

我敢肯定你會需要編寫原始SQL做連接。 – Falmarri

+0

在創建光標加載器時,我將如何編寫原始SQL查詢?它只接受那些字符串作爲參數(投影,選擇等) http://developer.android.com/reference/android/content/CursorLoader.html – urSus

回答

2

最簡單的解決方案是創建一個視圖,該視圖可以連接您的表並使用Uri從CursorLoader訪問視圖。

0
Cursor c = db.query(
    RefuelTable.TABLE_NAME + " , " + ExpenseTable.TABLE_NAME, 
    Utils.concat(RefuelTable.PROJECTION, ExpenseTable.PROJECTION), 
    RefuelTable.EXP_ID + " = " + ExpenseTable.ID + " AND " + RuelTable.ID + " = " + id , 
    null, 
    null, 
    null, 
    null); 

不喜歡這