연락처 보여주는 Fragment
app 에서 contacts list(주소록) 을 불러와서 써야 할 때가 있다. 이것을 손쉽게 하는 방법이 구글 개발자 문서(아래)에 나와 있다.
여기에 보면 소스를 다운로드 할 수 있다. 오래된? 소스라 gradle 로 작성되어 있지는 않다.
여하튼 fragment 로 구현되어 있어서, 이녀석을 가져다 원하는 곳에 사용하면 된다.
전화번호만 있는 연락처 보여주기
간혹 특정 조건에 맞는 연락처를 보여주고 싶을 때가 있다. 그럴 경우는 fragment 를 조금 수정하면 된다. 이 fragment 가 LoaderManager 를 이용하는데, 그래서 onCreateLoader 부분을 조금 수정하면 된다.아래에는 전화번호가 있는 연락처만 query 하도록 수정했다.
public class ContactsListFragment extends ListFragment implements
AdapterView.OnItemClickListener, LoaderManager.LoaderCallbacks<Cursor> {
...
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// If this is the loader for finding contacts in the Contacts Provider
// (the only one supported)
if (id == ContactsQuery.QUERY_ID) {
...
return new CursorLoader(getActivity(),
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
null,
ContactsQuery.SORT_ORDER);
}
Log.e(TAG, "onCreateLoader - incorrect ID provided (" + id + ")");
return null;
}
/**
* This interface defines constants for the Cursor and CursorLoader, based on constants defined
* in the {@link Contacts} class.
*/
public interface ContactsQuery {
// An identifier for the loader
final static int QUERY_ID = 1;
// A content URI for the Contacts table
final static Uri CONTENT_URI = Contacts.CONTENT_URI;
// The search/filter query Uri
final static Uri FILTER_URI = Contacts.CONTENT_FILTER_URI;
// The selection clause for the CursorLoader query. The search criteria defined here
// restrict results to contacts that have a display name and are linked to visible groups.
// Notice that the search on the string provided by the user is implemented by appending
// the search string to CONTENT_FILTER_URI.
@SuppressLint("InlinedApi")
final static String SELECTION =
(Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
"<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1" +
" AND " + Contacts.HAS_PHONE_NUMBER + "=1";
...
}
}
댓글 없음:
댓글 쓰기