Android 装载器—使用LoaderManager的回调方法

时间 : 14-12-10 栏目 : Android, 移动开发 作者 : noway 评论 : 0 点击 : 316 次

LoaderManager.LoaderCallbacks是一个让客户与LoadManager进行交互的回调接口。
装载器,尤其是CursorLoader装载器,被期望用来保存被终止后的数据,这样就允许应用程序在Activity之间或Fragment的onStop()和onStart()方法之间进行切换时保存数据,以便在用户返回应用程序时,它们不需要因数据重载而等待。使用LoaderManager.LoaderCallbacks()的回调方法就知道在什么时候要创建一个新的装载器,并且告诉应用程序什么时候是终止使用装载器数据的时间。

LoaderManager.LoaderCallbacks接口包含以下方法:

1. onCreateLoader()---用给定的ID实例化并返回一个新的Loader对象;

2. onLoaderFinished()---当之前创建的装载器已经完成它的装载时,调用这个方法;

3. onLoaderReset()---当之前创建的装载器被重置时,调用这个方法,这样会使这个装载器的数据变的无效。

下面分别对这些方法进行详细说明。

onCreateLoader

当你想要访问一个装载器是时(例如,通过initLoader()方法),它会检查指定ID的装载器是否存在,如果不存在,它会触发LoaderManager.LoaderCallbacks的onCreateLoader()回调方法。这是创建一个新的装载器的地方,典型的是创建一个CursorLoader装载器,但是你能够实现你自己的Loader类的子类。

例如:

// If non-null, this is the current filter the user has provided.
String mCurFilter;
...
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// This is called when a new Loader needs to be created.  This
// sample only has one Loader, so we don't care about the ID.
// First, pick the base URI to use depending on whether we are
// currently filtering.
Uri baseUri;
if (mCurFilter != null) {
baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
Uri.encode(mCurFilter));
} else {
baseUri = Contacts.CONTENT_URI;
}

// Now create and return a CursorLoader that will take care of
// creating a Cursor for the data being displayed.
String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
+ Contacts.HAS_PHONE_NUMBER + "=1) AND ("
+ Contacts.DISPLAY_NAME + " != '' ))";
return new CursorLoader(getActivity(), baseUri,
CONTACTS_SUMMARY_PROJECTION, select, null,
Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

在这个列子中,onCreateLoader()回调方法创建了一个CursorLoader装载器,你必须使用构造器方法来创建这个CursorLoader对象,它需要一整套执行对ContentProvider的查询所需要信息,具体需要如下:

1.  uri---要获取的内容的位置(URI);

2.  projection---一个要返回的列的列表,如果传递null,将返回所有的列,这样是低效的。

3.  selection---一个要返回的行的过滤器声明,它使用SQL WHERE子句的格式(不包括WHERE关键字本身)。传递null时将返回给定URI的所有行数据。

4.  selectionArgs---你可以在selection中包含“?”字符,这些字符将会被selectionArgs中的值顺序替换。这些值将作为字符串被绑定。

5.  sortOrder---使用SQL ORDER BY子句的格式(不包括ORDER BY自己)指定行记录是如何排序的,如果传递null将会使用默认的排序,也可以是无序的。

onLoadFinished

当之前创建的装载器已经完成了它的加载时这个方法会被调用。在提供给这个装载器的最后的数据释放之前会保证调用这个方法。在这个时点你应该删除所有的对旧数据使用(因为数据即将被释放),但是你不应该自己做数据的释放,因为装载器本身会做这件事情。

装载器一旦知道应用程序不在使用它,就会释放数据。例如,如果数据是来自CursorLoader对象的一个游标,你不应该自己来调用close()方法,如果游标被放到了CursorAdapter中,你应该使用swapCursor()方法,以便旧的Cursor对象不被关闭。如:

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
...

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
// Swap the new cursor in.  (The framework will take care of closing the
// old cursor once we return.)
mAdapter.swapCursor(data);
}

 

onLoaderReset

当之前被创建的装载器被重置的时候,这个方法会被调用。这样就是它的数据变的无效。这个回调能够让你找到数据被释放的时机,以便你能够在数据被释放之前删除对它的引用。

用带有null参数的swapCursor()方法实现这样的调用,如:

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
...

public void onLoaderReset(Loader<Cursor> loader) {
// This is called when the last Cursor provided to onLoadFinished()
// above is about to be closed.  We need to make sure we are no
// longer using it.
mAdapter.swapCursor(null);
}

本文标签

除非注明,文章均为( noway )原创,转载请保留链接: http://blog-old.z3a105.com/?p=349

Android 装载器—使用LoaderManager的回调方法:等您坐沙发呢!

发表评论





       ==QQ:122320466==

 微信    QQ群


0

Aujourd’hui, une partie avec le développement du e-commerce, achats en ligne est devenu une partie de la vie pour beaucoup de gens. La mariage a commencé achats en ligne. Si vous choisissez achats les mariages en ligne, il peut être beaucoup moins cher que le salon de la Robe de mariée pas chermariée local, réduisant le budget de mariage. vous pouvez avoir beaucoup de choix si acheter de mariage en ligne. vous pouvez ramasser une robe de mariée bon marché sur Internet.
Piercing fascinerande figur, och nu tittar vi på 2016 senast brudklänning, kan du vara den vackraste bruden det!2016 senaste Bra brudklänning, söt temperament Bra design, romantiska spetsar blomma kjol, som du lägger till en elegant och charmig temperament.Kvinnan tillbaka mjuka linjer, människor brudklänningofta få en känsla av oändlig frestelse. Fall 2016 mässan, lämnar uppgifter om ditt bröllop charmig.
Yesterday afternoon, the Chinese team was training in the Guangzhou Gymnasium, when the reporter asked Zhao Yunlei the feeling of wearing the new cheap jersey , cheap jerseys online shopshe readily took a shirt from the bag crumpled ball to reporters, and she said with a smile: ” This shirt is light. ”Zhao Yunlei said: “Our material is very light like with the clothes of the tennis King Nadal, Federer, after the sweat, sweat does not drip down to the ground, when we do move, it is easy pace slipping if the sweat drip on the floor.”Tennis players Zhang Yawen, told reporters: “You might think the clothes attached to the body, fearing we swing will be affected, in fact, we do not feel anything, because the clothes are very light, very soft, put on quite comfortable. And it’s particularly good clothes to dry, washing and will dry in 15 minutes. ”
China’s sports enthusiasts NFL sweatshirt with mad love and the pursuit of, and therefore, NFL jerseys have a good market in China and development. China is a populous country, is the consumer, the economic momentum is so good, the sales prospects sportswear is immeasurable. With hot sales sweatshirt, but also to promote the importance of sports fans, on health, on the other hand is a matter of concern for the World Cup, fans wearing NFL jerseys and also can express themselves more fully love and obsession Therefore, NFL jerseys Wholesale jerseys online shopwholesale has good prospects and development in China.
ANTA-ANTA Sports Products Limited, referred to as ANTA Sports, Anta, is China’s leading sporting goods companies, mainly engaged in the design, development, manufacture and marketing of ANTA brand sporting goods, including sports footwear, apparel and accessories. Anta sweatshirt design advantages, warm stretch knit fabric, using Slim version of model, more personal fit, bid farewell to bloated, so wearing more stylish.GUIRENNIAO-This logo is a spiritual totem, smooth graphics implication unstoppable force; flexible deliver an elegant arc Wholesale jerseys china shop movement, strength and speed of the United States, a symbol of passion and rationality publicity “Heart” and “meaning”, “concept” unity; pass the fearless and enterprising mind, showing beyond the realm of self, to unstoppable force to create the future.XTEP-Xtep (China) Co., Ltd. is a comprehensive development wholesale jerseys china shop, production and marketing of Xtep brand (XTEP) sports shoes, clothing, bags, caps, balls, socks mainly large sporting goods industry enterprises.
There are a lot of fans in identifying the authenticity of the above cheap jerseys have great distress, so here to i will show you some methods to definitely affordable inexpensive cheap jerseys : Firstly, we should look at if it is working fine. China has been called the world’s factory, a lot cheap jerseys factories in China have foundries, but our cheap jerseys are all from here! Secondly, should to see whether it is the , we all know that it is difficult to get out of print once a genuine cheap cheap jerseys free shipping jersey was print. and we have all kind of stocka on the whole website, in other words, we have all you want ! Finally, look at the price, our price is not necessarily the lowest in the whole website but it must be most fair on the whole website, we certainly you will not regret later when you buy it. Of course, except that cheap jerseys, we also have the other products, such as socks, leggings and some other related products, everyone can enjoy the best services of here!

KUBET