안드로이드 연락처 초성(한글)검색을 만들어 봤습니다.
Android SDK 1.5 기준 입니다.
SDK 2.0을 오늘(2009.12.21) 받았는데.... Contacts API들이 deprecated 되었군요... ㅜㅜ
간단한 프로그램인데, 처음 만들어 보는거라 그런지 이것저것 삽질을 많이 했네요....
소스 다운로드 :
어플 다운로드 : ContactsManager.apk




전화번호 목록은 어찌 생기는지 알려주세요...ㅠㅠ
이제 막 시작한 초보입니다...아무리 찾아도 전화번호 등록하는곳은 없네여...
소스 분석하면서 정리해 봤습니다.
package com.ziofront.utils;
import java.util.Arrays;
/*
* com.ziofront.utils
* HangulUtils.java
* Jiho Park 2009. 11. 27.
*
* Copyright (c) 2009 ziofront.com. All Rights Reserved.
*/
public class HangulUtils {
public static final char HANGUL_BEGIN_UNICODE = 44032; // 가
public static final char HANGUL_END_UNICODE = 55203; // 힣
public static final char HANGUL_BASE_UNIT = 588;//각자음 마다 가지는 글자수
public static final char[] INITIAL_SOUND = { 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ',
'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' };
private static char getInitalSound(char hanChar) {
int hanBegin = (hanChar - HANGUL_BEGIN_UNICODE);
int index = hanBegin / HANGUL_BASE_UNIT;
return INITIAL_SOUND[index];
}
private static boolean isHangul(char unicode) {
return HANGUL_BEGIN_UNICODE <= unicode && unicode <= HANGUL_END_UNICODE;
}
private static boolean containInitialChar(char c) {
return Arrays.binarySearch(INITIAL_SOUND, c) != -1;
}
public static String getHangulInitialSound(String value, String search) {
StringBuilder sb = new StringBuilder();
int minLen = Math.min(value.length(), search.length());
for (int i=0; i<minLen; i++) {
char ch = value.charAt(i);
if (isHangul(ch) && containInitialChar(search.charAt(i))) {
sb.append(getInitalSound(ch));
}else{
sb.append(ch);
}
}
return sb.toString();
}
public static boolean isHangulInitialSound(String value, String sarch) {
if(value == null || sarch == null){
if(value != null) return true;
return sarch == value;
}
return sarch.equals(getHangulInitialSound(value, sarch));
}
public static void main(String[] args) {
System.out.println(isHangulInitialSound("ㄱ", "ㄱㄴ") == false);
System.out.println(isHangulInitialSound("ㄱ나다라", "ㄱㄴ") == true);
System.out.println(isHangulInitialSound("가나다라", "ㄱㄴ") == true);
System.out.println(isHangulInitialSound("abc", "ab") == true);
System.out.println(isHangulInitialSound("abc", null) == true);
System.out.println(isHangulInitialSound(null, null) == true);
System.out.println(isHangulInitialSound(null, "abc") == false);
}
}
한가지 물어보고 싶어서 글을 올립니다.
2.1버전으로 수정했을 때
String[] projection = new String[] { Contacts.People._ID,
Contacts.People.NAME, Contacts.People.NUMBER };
Cursor cursor = managedQuery(Contacts.People.CONTENT_URI, projection,
null, null, null);
이 부분을
String[] projection = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, projection,
null, null, null);
이렇게 수정해봤습니다. 다른 방법으로도 많이 해봤지만 결과는1.5버전의 Contacts.People.NUMBER 요것을 2.1
버전으로 어떻게 해야할지 몰라서 몇 일을 헤메고 있습니다..ㅠㅠ 혹시 아시는분 있으면 정중하게 부탁드립니다..
Contacts.People.NAME, Contacts.People.NUMBER };
Cursor cursor = managedQuery(Contacts.Phones.CONTENT_URI, projection,
null, null, null);
이렇게 하면 되긴 되는데.... 더 좋은 방법이 있으면 답변부탁드립니다.
제가 지금 이거 때문에 머리가 아픈데 ㅠㅠ
duck5656@nate.com
여기로 메일 한통만 보내주시면 안될까요 ㅠㅠㅠㅠㅠㅠ....
꼭 부탁드립니다
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cursor = managedQuery(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection,
null, null, null);
2.2. 에서 이렇게 해결하였습니다.
눌렀을 때 이벤트 발생까지는 이제 만들었네요.
전화와 문자를 보낼 수 있도록 해놨습니다.
그런데 문제인게 검색을 했을 때, position 들이 달라지는데,
그 때 바뀐 값들을 다시 적용해야 하는데 그 부분이 않되서 문제네요.
초성비교 간단하게~ 바꾼것.
private static final char[] initList = { 'ㄱ', 'ㄲ', 'ㄴ', 'ㄷ', 'ㄸ', 'ㄹ',
'ㅁ', 'ㅂ', 'ㅃ', 'ㅅ', 'ㅆ', 'ㅇ', 'ㅈ', 'ㅉ', 'ㅊ', 'ㅋ', 'ㅌ', 'ㅍ', 'ㅎ' };
public boolean test(String test1, String test2) {
boolean result = true;
if(test2 == null || test1.equals(test2))
{ // 무조건 통과
return true;
}
else if( test1.length() < test2.length() )
{ // 무조건 실패
return false;
}
else
{ // 한글자씩 검사
int length = test2.length();
for(int i = 0; i < length; i++)
{
char char1 = test1.charAt(i), char2 = test2.charAt(i);
if( (char2 >= 'ㄱ' && char2 <= 'ㅎ') && !(char1 >= 'ㄱ' && char1 <= 'ㅎ'))
{ // 입력문자가 초성이고 비교 문자가 초성이 아닌경우
char1 = initList[(char1-'가') / 588]; // 588 = 하나의 초성이 가질 수 있는 갯수
}
if( char1 != char2 )
{
result = false;
break;
}
}
}
return result;
}