마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

안드로이드 주소록[전화번호부] 중복데이터.

0 추천
package com.example.getperson;

import java.util.ArrayList;

import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.database.Cursor;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        // * 데이터 원본 준비
        ArrayList<Contact> arContactList = new ArrayList<Contact>();        
        arContactList = getContactList();
        ArrayList<String> arGeneral = new ArrayList<String>();
        for( int i = 0 ; i < arContactList.size() ; i++ )
        {
            arGeneral.add( arContactList.get(i).name );
        }
        // */
 
        /*
         * 배열로 준비 String[] arGeneral = {"김유신", "이순신", "강감찬", "을지문덕"}; //
         */
 
        // 어댑터 준비
        ArrayAdapter<String> Adapter;
        Adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, arGeneral);
 
        // 어댑터 연결
        ListView list = (ListView) findViewById(R.id.list);
        list.setAdapter(Adapter);
    }
 
    private ArrayList<Contact> getContactList() {
 
        Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
 
        String[] projection = new String[] {
 
        ContactsContract.CommonDataKinds.Phone.CONTACT_ID, // 연락처 ID -> 사진 정보
                                                            // 가져오는데 사용
 
                ContactsContract.CommonDataKinds.Phone.NUMBER, // 연락처
 
                ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME }; // 연락처
                                                                        // 이름.
 
        String[] selectionArgs = null;
 
        String sortOrder = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
 
        + " COLLATE LOCALIZED ASC";//내림차순, 
 
        Cursor contactCursor = managedQuery(uri, projection, null,
 
        selectionArgs, sortOrder);
 
        ArrayList<Contact> contactlist = new ArrayList<Contact>();
 
        if (contactCursor.moveToFirst()) {
 
            do {
 
                String phonenumber = contactCursor.getString(1).replaceAll("-",
 
                "");
 
                if (phonenumber.length() == 10) {
 
                    phonenumber = phonenumber.substring(0, 3) + "-"
 
                    + phonenumber.substring(3, 6) + "-"
 
                    + phonenumber.substring(6);
 
                } else if (phonenumber.length() > 8) {
 
                    phonenumber = phonenumber.substring(0, 3) + "-"
 
                    + phonenumber.substring(3, 7) + "-"
 
                    + phonenumber.substring(7);
 
                }
 
                Contact acontact = new Contact();
 
                acontact.setPhotoid(contactCursor.getLong(0));
 
                acontact.setPhonenum(phonenumber);
 
                acontact.setName(contactCursor.getString(2));
 
                contactlist.add(acontact);
 
            } while (contactCursor.moveToNext());
 
        }
 
        return contactlist;
 
    }
 
    public class Contact {
        long photoid;
        String phonenum;
        String name;
         
        public long getPhotoid() {
            return photoid;
        }
        public void setPhotoid(long photoid) {
            this.photoid = photoid;
        }
        public String getPhonenum() {
            return phonenum;
        }
        public void setPhonenum(String phonenum) {
            this.phonenum = phonenum;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
         
         
    }
 
 
}

안녕하세요,

안드로이드 전화번호부 예제를 공부 중인데, 일단 인터넷에서 찾은 예제물로 분석하고있는데요,

앱을 실행해보니 연락처가 2개 이상 저장된 사람이 2번 나오는데요..[가령 한 사람이 집주소/전화번호 갖고있는 경우]

무엇이 문제인지 확인해보니.. HAS_PHONE_NUMBER와 NUMBER일 때가 다릅니다.

전자일 경우엔 한사람만 나오지만, 후자일 경우엔 위에서 말씀드린대로 한사람이 두개 이상 연락처를 갖고 있으면

두번 출력되네요.. 두 개의 차이점이 정확하게 뭔지 궁금합니다 ㅠ

anci (19,950 포인트) 님이 2014년 2월 24일 질문

답변 달기

· 글에 소스 코드 보기 좋게 넣는 법
· 질문에 대해 추가적인 질문이나 의견이 있으면 답변이 아니라 댓글로 달아주시기 바랍니다.
표시할 이름 (옵션):
개인정보: 당신의 이메일은 이 알림을 보내는데만 사용됩니다.
스팸 차단 검사:
스팸 검사를 다시 받지 않으려면 로그인하거나 혹은 가입 하세요.
...