상세 컨텐츠

본문 제목

android 디바이스에 저장된 연락처 가져오기

Android

by 잘구운토스트 2017. 10. 17. 14:40

본문

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
String[] addressInfo = {
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
    ContactsContract.CommonDataKinds.Phone.NUMBER,
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
};
 
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
 
// 한글/영문/숫자및특수문자 순으로 정렬
String sortOrder = "case" +
    " when substr(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ", 1, 1) BETWEEN 'ㄱ' AND '힣'  then 1 " +
    " when substr(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ", 1, 1) BETWEEN 'A' AND 'Z' then 2 " +
    " when substr(" + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + ", 1, 1) BETWEEN 'a' AND 'z' then 3 " +
    " else 4 end, " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
 
Cursor contactCursor = context.getContentResolver().query(uri, addressInfo, nullnull, sortOrder);
 
List<ContactVO> _list = new ArrayList<>();
 
if (contactCursor != null) {
 
    while (contactCursor.moveToNext()) {
 
        String phoneNum = contactCursor.getString(1);
        String name = contactCursor.getString(2);
 
    }
    contactCursor.close();
}
cs



ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " COLLATE LOCALIZED ASC"

만 추가하면 숫자/한글/영문 순으로 정렬된다.

하여 한글/영문/숫자 순으로 정렬하기 위해서 위 소스처럼 case문으로 처리해야한다.

관련글 더보기

댓글 영역