You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android联系人搜索功能异常:LoaderManager相关代码求助

排查联系人搜索Loader功能异常问题

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 at ContentResolver 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 the Bundle doesn't contain the QUERY_KEY value, query will 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 the READ_CONTACTS permission. First, add it to your AndroidManifest.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 ContentResolver will return an empty cursor.

  • Ensure full implementation of LoaderCallbacks
    You only shared the onCreateLoader method—don't forget to properly implement onLoadFinished and onLoaderReset to 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 in onCreateLoader to confirm that query is being set correctly:

    Log.d(TAG, "Received search query: " + query);
    

    If the log shows query is null or empty, the issue is with how you're passing the argument to the Loader.

内容的提问来源于stack exchange,提问作者platizin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 03:41:18