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일 때가 다릅니다.
전자일 경우엔 한사람만 나오지만, 후자일 경우엔 위에서 말씀드린대로 한사람이 두개 이상 연락처를 갖고 있으면
두번 출력되네요.. 두 개의 차이점이 정확하게 뭔지 궁금합니다 ㅠ