Search code examples
androidandroid-cursorloaderandroid-loader

How does a CursorAdapter load data from Cursor provided by CursorLoader in Android?


I have implemented the following code to display Contacts as a ListView. MyAdapter extends CursorAdapter.

public class MainActivity extends FragmentActivity implements 
    LoaderManager.LoaderCallbacks<Cursor>{

    private MyAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ListView lv = (ListView)findViewById(R.id.listView_val);
        adapter = new MyAdapter(this, null, 1);
        lv.setAdapter(adapter);

        .....

        getSupportLoaderManager().initLoader(0, null, this);
    } 
        .....
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) { ..... } ....}

What I want to know is, how does the adapter - mAdapter know that it has to pick the data from the cursor supplied by the CursorLoader (Cursor available after calling onCreateLoader)?


Solution

  • http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html#onLoadFinished(android.content.Loader, D) here you set the cursor to the adapter by calling swapCursor