ITKeyword,专注技术干货聚合推荐

注册 | 登录

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

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

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

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

 | 

2 Answers
2

解决方法

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


|
  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      google.com/… –  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. http://developer.android.com/training/load-data-background/setup-loader.html


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

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

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

如果您没有收到激活邮件,请注意检查垃圾箱。