@@ -147,14 +147,19 @@ protected void initialize(
147
147
setListAdapter (mAdapter );
148
148
}
149
149
150
+ private static enum ListingMode {
151
+ DELETE_EXISTING_OBJECTS ,
152
+ ADD_TO_EXISTING_OBJECTS ,
153
+ };
154
+
150
155
private static class ListingStatus <T > {
151
- public ListingStatus (T [] o , boolean d ) {
156
+ public ListingStatus (T [] o , ListingMode m ) {
152
157
objects = o ;
153
- deleteExistingObjects = d ;
158
+ mode = m ;
154
159
}
155
160
156
161
public final T [] objects ;
157
- public final boolean deleteExistingObjects ;
162
+ public final ListingMode mode ;
158
163
}
159
164
160
165
/**
@@ -251,20 +256,25 @@ protected String doInBackground(Integer... mode) {
251
256
} else {
252
257
r = mCache .read (mCacheKey , mMaxCacheStalenessMillis );
253
258
}
254
- final boolean hitCache = (r .obj != null );
259
+ final boolean hitCache = (r .obj != null );
255
260
if (hitCache ) {
256
- publishProgress (new ListingStatus <T >((T [])r .obj , false ));
261
+ publishProgress (new ListingStatus <T >((T [])r .obj , ListingMode . DELETE_EXISTING_OBJECTS ));
257
262
}
263
+
258
264
if (r .needRefresh ) {
259
265
ArrayList <T > aggregate = new ArrayList <T >();
260
266
fetcher = new ParallelFetcher ();
267
+ boolean firstPublish = true ;
261
268
while (fetcher .hasNext ()) {
262
269
T [] objs = fetcher .next ();
263
270
if (objs != null ) {
264
271
for (T obj : objs ) aggregate .add (obj );
265
272
if (!hitCache ) {
266
273
// Incrementally update the screen as results arrive
267
- publishProgress (new ListingStatus <T >(objs , false ));
274
+ publishProgress (new ListingStatus <T >(objs ,
275
+ (firstPublish ? ListingMode .DELETE_EXISTING_OBJECTS :
276
+ ListingMode .ADD_TO_EXISTING_OBJECTS )));
277
+ firstPublish = false ;
268
278
} else {
269
279
// If the screen was already filled with a stale cache,
270
280
// buffer the new contents until it is complete, so that
@@ -276,7 +286,7 @@ protected String doInBackground(Integer... mode) {
276
286
T [] objs = aggregate .toArray (mTmpArray );
277
287
if (mCacheKey != null ) mCache .write (mCacheKey , objs );
278
288
if (hitCache ) {
279
- publishProgress (new ListingStatus <T >(objs , true ));
289
+ publishProgress (new ListingStatus <T >(objs , ListingMode . DELETE_EXISTING_OBJECTS ));
280
290
}
281
291
}
282
292
}
@@ -297,7 +307,7 @@ protected String doInBackground(Integer... mode) {
297
307
@ Override
298
308
protected void onProgressUpdate (ListingStatus <T >... list ) {
299
309
for (ListingStatus <T > status : list ) {
300
- if (status .deleteExistingObjects ) {
310
+ if (status .mode == ListingMode . DELETE_EXISTING_OBJECTS ) {
301
311
mAdapter .setObjects (status .objects );
302
312
} else {
303
313
mAdapter .addObjects (status .objects );
0 commit comments