Android联系人搜索功能异常:LoaderManager相关代码求助
Hey there, let's figure out why your contact search using LoaderManager.LoaderCallbacks isn't working as expected. Based on the code snippet you shared, here are key areas to check and fix:
Complete your ContentResolver query logic
Your code cuts off atContentResolver cr ...—this is the core of fetching contact data. Make sure you're building the query correctly with the right contact URI, projection, selection criteria, and arguments. Here's a proper example:@Override public Loader<Cursor> onCreateLoader(int loaderIndex, Bundle args) { query = args.getString(QUERY_KEY); ContentResolver cr = mContext.getContentResolver(); // Use the correct URI for phone contacts Uri contactUri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; // Define columns you need to retrieve String[] projection = { ContactsContract.CommonDataKinds.Phone._ID, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }; // Set up search condition with wildcard matching String selection = ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " LIKE ?"; String[] selectionArgs = new String[]{"%" + query + "%"}; return new CursorLoader(mContext, contactUri, projection, selection, selectionArgs, null); }Double-check that you're using the right contact URI and that your selection logic uses wildcards (
%) properly—without them, the search will only match exact strings instead of partial matches.Verify Loader initialization and argument passing
Ensure you're correctly passing the search query to the Loader when initializing it. If theBundledoesn't contain theQUERY_KEYvalue,querywill be null and no results will be returned. Here's how to properly start the Loader:// When triggering the search String searchQuery = "your_user_input_here"; Bundle args = new Bundle(); args.putString(Callbacks.QUERY_KEY, searchQuery); getLoaderManager().initLoader(0, args, new Callbacks(this));Check contact permissions
Contact search requires theREAD_CONTACTSpermission. First, add it to yourAndroidManifest.xml:<uses-permission android:name="android.permission.READ_CONTACTS" />For Android 6.0 (API level 23) and above, you also need to request this permission dynamically at runtime. If you skip this step, the
ContentResolverwill return an empty cursor.Ensure full implementation of LoaderCallbacks
You only shared theonCreateLoadermethod—don't forget to properly implementonLoadFinishedandonLoaderResetto handle the cursor results:@Override public void onLoadFinished(Loader<Cursor> loader, Cursor data) { // Bind the cursor to your UI component (e.g., ListView adapter) if (data != null && !data.isClosed()) { // Example: Update your adapter with the new cursor yourContactAdapter.swapCursor(data); } } @Override public void onLoaderReset(Loader<Cursor> loader) { // Clear the cursor reference to avoid memory leaks yourContactAdapter.swapCursor(null); }Without these methods, even if the Loader fetches data, you won't see it in your UI.
Debug the query value
Add a log statement inonCreateLoaderto confirm thatqueryis being set correctly:Log.d(TAG, "Received search query: " + query);If the log shows
queryis null or empty, the issue is with how you're passing the argument to the Loader.
内容的提问来源于stack exchange,提问作者platizin

