development
修订版 | 69c72dfdf901e07a132f3ad4856c2058e5efa2e9 (tree) |
---|---|
时间 | 2011-01-09 11:34:43 |
作者 | Dianne Hackborn <hackbod@goog...> |
Commiter | Dianne Hackborn |
Clean up from comments, enable throttling.
Change-Id: I719b0491a7953ab57d76634699c6fdf3ad8558e0
@@ -343,7 +343,8 @@ | ||
343 | 343 | </activity> |
344 | 344 | |
345 | 345 | <!-- Loader Samples --> |
346 | - | |
346 | + | |
347 | +<!-- BEGIN_INCLUDE(loader_throttle) --> | |
347 | 348 | <activity android:name=".app.LoaderThrottle" |
348 | 349 | android:label="@string/loader_throttle"> |
349 | 350 | <intent-filter> |
@@ -353,6 +354,7 @@ | ||
353 | 354 | </activity> |
354 | 355 | <provider android:name=".app.LoaderThrottle$SimpleProvider" |
355 | 356 | android:authorities="com.example.android.apis.app.LoaderThrottle" /> |
357 | +<!-- END_INCLUDE(loader_throttle) --> | |
356 | 358 | |
357 | 359 | <!-- Intent Samples --> |
358 | 360 |
@@ -16,6 +16,7 @@ | ||
16 | 16 | |
17 | 17 | package com.example.android.apis.app; |
18 | 18 | |
19 | +//BEGIN_INCLUDE(complete) | |
19 | 20 | import android.app.Activity; |
20 | 21 | import android.app.FragmentManager; |
21 | 22 | import android.app.ListFragment; |
@@ -29,6 +30,7 @@ import android.content.CursorLoader; | ||
29 | 30 | import android.content.Loader; |
30 | 31 | import android.content.UriMatcher; |
31 | 32 | import android.database.Cursor; |
33 | +import android.database.DatabaseUtils; | |
32 | 34 | import android.database.SQLException; |
33 | 35 | import android.database.sqlite.SQLiteDatabase; |
34 | 36 | import android.database.sqlite.SQLiteOpenHelper; |
@@ -56,12 +58,12 @@ import java.util.HashMap; | ||
56 | 58 | public class LoaderThrottle extends Activity { |
57 | 59 | // Debugging. |
58 | 60 | static final String TAG = "LoaderThrottle"; |
59 | - | |
61 | + | |
60 | 62 | /** |
61 | 63 | * The authority we use to get to our sample provider. |
62 | 64 | */ |
63 | 65 | public static final String AUTHORITY = "com.example.android.apis.app.LoaderThrottle"; |
64 | - | |
66 | + | |
65 | 67 | /** |
66 | 68 | * Definition of the contract for the main table of our provider. |
67 | 69 | */ |
@@ -86,12 +88,7 @@ public class LoaderThrottle extends Activity { | ||
86 | 88 | */ |
87 | 89 | public static final Uri CONTENT_ID_URI_BASE |
88 | 90 | = Uri.parse("content://" + AUTHORITY + "/main/"); |
89 | - | |
90 | - /** | |
91 | - * 0-relative position of a main ID segment in the path part of a main ID URI | |
92 | - */ | |
93 | - public static final int MAIN_ID_PATH_POSITION = 1; | |
94 | - | |
91 | + | |
95 | 92 | /** |
96 | 93 | * The MIME type of {@link #CONTENT_URI}. |
97 | 94 | */ |
@@ -107,14 +104,14 @@ public class LoaderThrottle extends Activity { | ||
107 | 104 | * The default sort order for this table |
108 | 105 | */ |
109 | 106 | public static final String DEFAULT_SORT_ORDER = "data COLLATE LOCALIZED ASC"; |
110 | - | |
107 | + | |
111 | 108 | /** |
112 | 109 | * Column name for the single column holding our data. |
113 | 110 | * <P>Type: TEXT</P> |
114 | 111 | */ |
115 | 112 | public static final String COLUMN_NAME_DATA = "data"; |
116 | 113 | } |
117 | - | |
114 | + | |
118 | 115 | /** |
119 | 116 | * This class helps open, create, and upgrade the database file. |
120 | 117 | */ |
@@ -189,7 +186,7 @@ public class LoaderThrottle extends Activity { | ||
189 | 186 | mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); |
190 | 187 | mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME, MAIN); |
191 | 188 | mUriMatcher.addURI(AUTHORITY, MainTable.TABLE_NAME + "/#", MAIN_ID); |
192 | - | |
189 | + | |
193 | 190 | // Create and initialize projection map for all columns. This is |
194 | 191 | // simply an identity mapping. |
195 | 192 | mNotesProjectionMap = new HashMap<String, String>(); |
@@ -227,8 +224,9 @@ public class LoaderThrottle extends Activity { | ||
227 | 224 | case MAIN_ID: |
228 | 225 | // The incoming URI is for a single row. |
229 | 226 | qb.setProjectionMap(mNotesProjectionMap); |
230 | - qb.appendWhere(MainTable._ID + "=" + uri.getPathSegments().get( | |
231 | - MainTable.MAIN_ID_PATH_POSITION)); | |
227 | + qb.appendWhere(MainTable._ID + "=?"); | |
228 | + selectionArgs = DatabaseUtils.appendSelectionArgs(selectionArgs, | |
229 | + new String[] { uri.getLastPathSegment() }); | |
232 | 230 | break; |
233 | 231 | |
234 | 232 | default: |
@@ -299,7 +297,7 @@ public class LoaderThrottle extends Activity { | ||
299 | 297 | |
300 | 298 | throw new SQLException("Failed to insert row into " + uri); |
301 | 299 | } |
302 | - | |
300 | + | |
303 | 301 | /** |
304 | 302 | * Handle deleting data. |
305 | 303 | */ |
@@ -322,14 +320,8 @@ public class LoaderThrottle extends Activity { | ||
322 | 320 | case MAIN_ID: |
323 | 321 | // If URI is for a particular row ID, delete is based on incoming |
324 | 322 | // data but modified to restrict to the given ID. |
325 | - finalWhere = MainTable._ID + " = " + uri.getPathSegments().get( | |
326 | - MainTable.MAIN_ID_PATH_POSITION); | |
327 | - | |
328 | - if (where != null) { | |
329 | - // Combine with incoming where, if specified. | |
330 | - finalWhere = finalWhere + " AND " + where; | |
331 | - } | |
332 | - | |
323 | + finalWhere = DatabaseUtils.concatenateWhere( | |
324 | + MainTable._ID + " = " + ContentUris.parseId(uri), where); | |
333 | 325 | count = db.delete(MainTable.TABLE_NAME, finalWhere, whereArgs); |
334 | 326 | break; |
335 | 327 |
@@ -360,17 +352,11 @@ public class LoaderThrottle extends Activity { | ||
360 | 352 | case MAIN_ID: |
361 | 353 | // If URI is for a particular row ID, update is based on incoming |
362 | 354 | // data but modified to restrict to the given ID. |
363 | - finalWhere = MainTable._ID + " = " + uri.getPathSegments().get( | |
364 | - MainTable.MAIN_ID_PATH_POSITION); | |
365 | - | |
366 | - if (where != null) { | |
367 | - // Combine with incoming where, if specified. | |
368 | - finalWhere = finalWhere + " AND " + where; | |
369 | - } | |
370 | - | |
355 | + finalWhere = DatabaseUtils.concatenateWhere( | |
356 | + MainTable._ID + " = " + ContentUris.parseId(uri), where); | |
371 | 357 | count = db.update(MainTable.TABLE_NAME, values, finalWhere, whereArgs); |
372 | 358 | break; |
373 | - | |
359 | + | |
374 | 360 | default: |
375 | 361 | throw new IllegalArgumentException("Unknown URI " + uri); |
376 | 362 | } |
@@ -380,13 +366,13 @@ public class LoaderThrottle extends Activity { | ||
380 | 366 | return count; |
381 | 367 | } |
382 | 368 | } |
383 | - | |
369 | + | |
384 | 370 | @Override |
385 | 371 | protected void onCreate(Bundle savedInstanceState) { |
386 | 372 | super.onCreate(savedInstanceState); |
387 | 373 | |
388 | 374 | FragmentManager fm = getFragmentManager(); |
389 | - | |
375 | + | |
390 | 376 | // Create the list fragment and add it as our sole content. |
391 | 377 | if (fm.findFragmentById(android.R.id.content) == null) { |
392 | 378 | ThrottledLoaderListFragment list = new ThrottledLoaderListFragment(); |
@@ -400,7 +386,7 @@ public class LoaderThrottle extends Activity { | ||
400 | 386 | // Menu identifiers |
401 | 387 | static final int POPULATE_ID = Menu.FIRST; |
402 | 388 | static final int CLEAR_ID = Menu.FIRST+1; |
403 | - | |
389 | + | |
404 | 390 | // This is the Adapter being used to display the list's data. |
405 | 391 | SimpleCursorAdapter mAdapter; |
406 | 392 |
@@ -409,11 +395,11 @@ public class LoaderThrottle extends Activity { | ||
409 | 395 | |
410 | 396 | // Task we have running to populate the database. |
411 | 397 | AsyncTask<Void, Void, Void> mPopulatingTask; |
412 | - | |
398 | + | |
413 | 399 | @Override public void onActivityCreated(Bundle savedInstanceState) { |
414 | 400 | super.onActivityCreated(savedInstanceState); |
415 | 401 | |
416 | - setEmptyText("No data"); | |
402 | + setEmptyText("No data. Select 'Populate' to fill with data from Z to A at a rate of 4 per second."); | |
417 | 403 | setHasOptionsMenu(true); |
418 | 404 | |
419 | 405 | // Create an empty adapter we will use to display the loaded data. |
@@ -422,7 +408,7 @@ public class LoaderThrottle extends Activity { | ||
422 | 408 | new String[] { MainTable.COLUMN_NAME_DATA }, |
423 | 409 | new int[] { android.R.id.text1 }, 0); |
424 | 410 | setListAdapter(mAdapter); |
425 | - | |
411 | + | |
426 | 412 | // Prepare the loader. Either re-connect with an existing one, |
427 | 413 | // or start a new one. |
428 | 414 | getLoaderManager().initLoader(0, null, this); |
@@ -437,7 +423,7 @@ public class LoaderThrottle extends Activity { | ||
437 | 423 | |
438 | 424 | @Override public boolean onOptionsItemSelected(MenuItem item) { |
439 | 425 | final ContentResolver cr = getActivity().getContentResolver(); |
440 | - | |
426 | + | |
441 | 427 | switch (item.getItemId()) { |
442 | 428 | case POPULATE_ID: |
443 | 429 | if (mPopulatingTask != null) { |
@@ -465,7 +451,7 @@ public class LoaderThrottle extends Activity { | ||
465 | 451 | }; |
466 | 452 | mPopulatingTask.execute((Void[])null); |
467 | 453 | return true; |
468 | - | |
454 | + | |
469 | 455 | case CLEAR_ID: |
470 | 456 | if (mPopulatingTask != null) { |
471 | 457 | mPopulatingTask.cancel(false); |
@@ -479,7 +465,7 @@ public class LoaderThrottle extends Activity { | ||
479 | 465 | }; |
480 | 466 | task.execute((Void[])null); |
481 | 467 | return true; |
482 | - | |
468 | + | |
483 | 469 | default: |
484 | 470 | return super.onOptionsItemSelected(item); |
485 | 471 | } |
@@ -499,7 +485,7 @@ public class LoaderThrottle extends Activity { | ||
499 | 485 | @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { |
500 | 486 | CursorLoader cl = new CursorLoader(getActivity(), MainTable.CONTENT_URI, |
501 | 487 | PROJECTION, null, null, null); |
502 | - //cl.setUpdateThrottle(2000); // update at most every 2 seconds. | |
488 | + cl.setUpdateThrottle(2000); // update at most every 2 seconds. | |
503 | 489 | return cl; |
504 | 490 | } |
505 | 491 |
@@ -512,3 +498,4 @@ public class LoaderThrottle extends Activity { | ||
512 | 498 | } |
513 | 499 | } |
514 | 500 | } |
501 | +//END_INCLUDE(complete) |