解决How does a CursorAdapter load data from Cursor provided by CursorLoader in Android?
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
)?
|
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
|
相关阅读排行
- 1Android Https相关完全解析 当OkHttp遇到Https
- 2Android APK反编译详解(附图)
- 3[置顶] Android APK反编译就这么简单 详解(附图)
- 4Android Fragment 真正的完全解析(上)
- 5Android RecyclerView 使用完全解析 体验艺术般的控件