【发布时间】:2015-03-24 06:04:06
【问题描述】:
我的 android 应用程序应该加载设备中记录的所有联系人并将它们显示在列表中。我无法弄清楚原因,但并非所有联系人都已加载。
这是我用来通过 CursorLoader 启动查询的代码:
public android.support.v4.content.Loader<Cursor> onCreateLoader(int id, Bundle args) {
if (id == ContactsQuery.QUERY_ID) {
Uri contentUri;
if (mSearchTerm == null) {
contentUri = ContactsQuery.CONTENT_URI;
} else {
contentUri =
Uri.withAppendedPath(ContactsQuery.FILTER_URI, Uri.encode(mSearchTerm));
}
return new CursorLoader(getActivity(),
contentUri,
ContactsQuery.PROJECTION,
ContactsQuery.SELECTION,
null,
ContactsQuery.SORT_ORDER);
ContactsQuery 定义如下:
public interface ContactsQuery {
final static int QUERY_ID = 1;
final static Uri CONTENT_URI = Contacts.CONTENT_URI;
final static Uri FILTER_URI = Contacts.CONTENT_FILTER_URI;
@SuppressLint("InlinedApi")
final static String SELECTION =
(Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME) +
"<>''" + " AND " + Contacts.IN_VISIBLE_GROUP + "=1";
@SuppressLint("InlinedApi")
final static String SORT_ORDER =
Utils.hasHoneycomb() ? Contacts.SORT_KEY_PRIMARY : Contacts.DISPLAY_NAME;
@SuppressLint("InlinedApi")
final static String[] PROJECTION = {
// The contact's row id
Contacts._ID,
Contacts.LOOKUP_KEY,
Utils.hasHoneycomb() ? Contacts.DISPLAY_NAME_PRIMARY : Contacts.DISPLAY_NAME,
Utils.hasHoneycomb() ? Contacts.PHOTO_THUMBNAIL_URI : Contacts._ID,
SORT_ORDER,
};
final static int ID = 0;
final static int LOOKUP_KEY = 1;
final static int DISPLAY_NAME = 2;
final static int PHOTO_THUMBNAIL_DATA = 3;
final static int SORT_KEY = 4;
}
为什么当 mSearchTerm 为空时没有加载所有联系人?
【问题讨论】:
标签: android android-contacts android-cursorloader