2010-02-22 24 views

回答

11

由於沒有人來回答我只是要告訴我如何解決這個問題。基本上我會爲每個案例創建自定義URI並通過selection參數中的條件。然後在ContentProvider#query裏面我會識別這個案例,並根據表名和選擇參數構建原始查詢。

下面是簡單的例子:

switch (URI_MATCHER.match(uri)) { 
    case TYPES: 
     table = TYPES_TABLE; 
     break; 
    case TYPES_DISTINCT: 
     return db.rawQuery("SELECT DISTINCT type FROM types", null); 
    default: 
     throw new IllegalArgumentException("Unknown URI " + uri); 
    } 
    return db.query(table, null, selection, selectionArgs, null, null, null); 
+10

只有當你有這樣的工作提供者的手... – njzk2 2011-09-29 14:55:43

+0

@ njzk2如果我正確理解你,你是說有可能獲得實際的聯繫人數據庫的實例? – 2017-02-25 14:35:59

+0

@Ilya_Gazman不,我在說這個解決方案只適用於你是實現內容提供者的人。 – njzk2 2017-02-26 01:30:13

38

查詢ContentResolver的,使用的時候你可以做很好的破解:

String selection = Models.SOMETHING + "=" + something + ") GROUP BY (" + Models.TYPE; 
+0

如果您無法訪問ContentProvider源代碼,那麼請注意。謝謝。 – 2011-02-17 07:53:59

+0

它非常容易且工作良好,但如何使用DISTINCT? – Amal 2011-06-10 17:57:29

+0

對不起,我的錯我用GROUP BY與列我想DISTINCT與我不擅長在SQL – Amal 2011-06-10 20:52:45

13

在你重寫ContentProvider查詢方法有一個特定的URI的映射使用不同。

然後使用SQLiteQueryBuilder並調用setDistinct(boolean)方法。

@Override 
public Cursor query(Uri uri, String[] projection, String selection, 
     String[] selectionArgs, String sortOrder) 
{ 
    SQLiteQueryBuilder qb = new SQLiteQueryBuilder(); 

    boolean useDistinct = false; 

    switch (sUriMatcher.match(uri)) 
    { 
    case YOUR_URI_DISTINCT: 
     useDistinct = true; 
    case YOUR_URI: 
     qb.setTables(YOUR_TABLE_NAME); 
     qb.setProjectionMap(sYourProjectionMap); 
     break; 

    default: 
     throw new IllegalArgumentException("Unknown URI " + uri); 
    } 

    // If no sort order is specified use the default 
    String orderBy; 
    if (TextUtils.isEmpty(sortOrder)) 
    { 
     orderBy = DEFAULT_SORT_ORDER; 
    } 
    else 
    { 
     orderBy = sortOrder; 
    } 
    // Get the database and run the query 
    SQLiteDatabase db = mDBHelper.getReadableDatabase(); 
      // THIS IS THE IMPORTANT PART! 
    qb.setDistinct(useDistinct); 
    Cursor c = qb.query(db, projection, selection, selectionArgs, null, null, orderBy); 

    if (c != null) 
    { 
     // Tell the cursor what uri to watch, so it knows when its source data changes 
     c.setNotificationUri(getContext().getContentResolver(), uri); 
    } 

    return c; 
} 
+1

什麼是mDBHelper? – Hamidreza 2015-02-27 06:58:56

-1

也許它更簡單地獲得不同的值, 嘗試你想進入投影表

String[] projection = new String[]{ 
       BaseColumns._ID, 
       "DISTINCT "+ Mediastore.anything.you.want 
}; 

列名前添加DISTINCT字,並用它作爲參數傳遞給查詢的方法內容解析器!

我希望能幫助你,因爲我之前有一些日子

+3

這對我不起作用。我的代碼:\t \t字符串[]投影=新的String [] { 「DISTINCT」 \t \t \t \t + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME}; \t \t光標CUR = cr.query( \t \t \t \t ContactsContract.CommonDataKinds.Phone.CONTENT_URI,投影, \t \t \t \t 「LENGTH(」 + ContactsContract.CommonDataKinds.Phone.NUMBER \t \t \t \t \t \t +「)> = 8」,null,sortOrder);我得到無效的列DISTINCT display_name – Maurice 2011-09-02 07:42:16

+0

嘗試在「DISTINCT」 - >「DISTINCT」中添加空格;在這個博客http://javment.blogspot.com/2011/07/how-to-get-distinct-values-from-android.html我已經做了一個簡單的教程,可以幫助你。 – javment 2011-09-04 08:15:19

+4

這不適合我。即使有空間 – 2012-07-12 21:14:15

0

在某些條件下同樣的問題,我們可以用「不同(列)」作爲選擇, 和它的工作完美。 但在某些情況下,它會引起異常。

當它引起的異常,我會用一個HashSet來存儲列值....

9

如果你想使用DISTINCT與SELECT更多然後一個欄,您需要使用GROUP BY。
迷你哈克在ContentResolver.query使用這樣的:

Uri uri = Uri.parse("content://sms/inbox"); 
     Cursor c = getContentResolver().query(uri, 
      new String[]{"DISTINCT address","body"}, //DISTINCT 
      "address IS NOT NULL) GROUP BY (address", //GROUP BY 
      null, null); 
     if(c.moveToFirst()){ 
      do{ 
       Log.v("from", "\""+c.getString(c.getColumnIndex("address"))+"\""); 
       Log.v("text", "\""+c.getString(c.getColumnIndex("body"))+"\""); 

      } while(c.moveToNext()); 
     } 

此代碼選擇每個從設備的收件箱中的發件人的最後一個短信。
注意:在GROUP BY之前,我們總是需要寫至少一個條件。 ContentResolver.query方法裏面 結果的SQL查詢字符串:

SELECT DISTINCT address, body FROM sms WHERE (type=1) AND (address IS NOT NULL) GROUP BY (address) 
+0

謝謝!你拯救了我的生命:) – 2016-04-11 15:04:15

5

雖然我沒有用集團通過,我已經在內容解析器查詢使用鮮明

Cursor cursor = contentResolver .query(YOUR_URI, new String[] {"Distinct "+ YOUR_COLUMN_NAME}, null, null, null);

+2

作品正確謝謝 – 2015-12-28 09:58:16

0
// getting sender list from messages into spinner View 
    Spinner phoneListView = (Spinner) findViewById(R.id.phone_list); 
    Uri uri = Uri.parse("content://sms/inbox");  
    Cursor c = getContentResolver().query(uri, new String[]{"Distinct address"}, null, null, null); 
    List <String> list; 
    list= new ArrayList<String>(); 
    list.clear(); 
    int msgCount=c.getCount(); 
    if(c.moveToFirst()) { 
     for(int ii=0; ii < msgCount; ii++) { 
      list.add(c.getString(c.getColumnIndexOrThrow("address")).toString()); 
      c.moveToNext(); 
     } 
    } 
    phoneListView.setAdapter(new ArrayAdapter<String>(BankActivity.this, android.R.layout.simple_dropdown_item_1line, list)); 
+0

你最好解釋你的答案 – 2016-02-10 07:58:50

0

添加投影DISTINCT關鍵字的工作對我來說,但是,只有當不同的關鍵字是第一個參數的工作:

String[] projection = new String[]{"DISTINCT " + DBConstants.COLUMN_UUID, ... };