注册 | 登录

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

itPublisher 分享于



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

public class MainActivity extends FragmentActivity implements 

    private MyAdapter adapter;
    protected void onCreate(Bundle savedInstanceState) {

        ListView lv = (ListView)findViewById(;
        adapter = new MyAdapter(this, null, 1);


        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)?

android android-cursorloader android-loader
  this question
asked Nov 11 '13 at 12:44 yadav_vi 504 1 7 25


2 Answers

解决方法, D) here you set the cursor to the adapter by calling swapCursor

  this answer
answered Nov 11 '13 at 12:59 pskink 12.7k 2 23 31      Can you explain what the purpose of onLoadFinished is? I have read it, but it doesn't seem to be quite clear. Thanks in advance! –  yadav_vi Nov 11 '13 at 13:06 1   OnLoadFinished is called when hmm operation of cursor loading has finished as its name says, now you can pass returned Cursor to the CursorAdapter by calling swapCursor –  pskink Nov 11 '13 at 13:16      Hmm, I got something of it. Anyway, if you do have some links that explain the Callback methods in detail, do post here. I am new to Android and it all seems a little confusing. –  yadav_vi Nov 11 '13 at 13:21… –  pskink Nov 11 '13 at 13:27      Hey, I got it. Initially while creating the CursorAdapter we pass the Cursor as null, however, in onLoadFinished we call swapCursor which (I think) replaces the initial cursor with the newly passed one. Here is a good explanation. –  yadav_vi Nov 11 '13 at 13:29


This link provides with detailed example how the cursor adapter loads the data from cursor loader. Pass through next page also in the web link for the full answer.

  this answer
answered Dec 9 '13 at 14:01 cruise 13 4









您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱