소곤소곤 ad

2012년 11월 14일 수요일

Android 전화번호부 이름과 전화번오 얻어오기

안드로이드 전화번호부 이름과 전화번호 얻어오기 최근 안드로이드 버전에서 ContactsContract.CommonDataKinds.Phone로 바뀐부분을 적용했다. string의 ArrayList를 파라미터로 받아 오도록 제작.

private void getContacts(ArrayList<String> alist)
{
    String [] arrProjection      = { ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME };
    String [] arrPhoneProjection = { ContactsContract.CommonDataKinds.Phone.NUMBER };

    Cursor clsCursor = getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, arrProjection, ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1" , null, null );

    while( clsCursor.moveToNext() )
    {
        String strContactId = clsCursor.getString( 0 );
        Cursor clsPhoneCursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, arrPhoneProjection, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + strContactId, null, null );
        while( clsPhoneCursor.moveToNext() )
        {
            alist.add(clsCursor.getString( 1 )  + ":" + clsPhoneCursor.getString( 0 ));
        }

        clsPhoneCursor.close();   
    }
    clsCursor.close( );
}

위 함수를 사용할 때에는 다음과 같이, Adaptor를 이용하여 ListView에 보여주도록 해 보았다.

     ListView list = (ListView) findViewById(R.id.listview1);

     final ArrayList<String> arr = new ArrayList<String>();
     getContacts(arr);

     final ArrayAdapter<String> aa = new ArrayAdapter<String> (this, android.R.layout.simple_list_item_1, arr);                
     list.setAdapter(aa);                                                              

댓글 없음:

댓글 쓰기