Top Banner
lec 04 Content Providers Adapters CursorLoaders Advanced Debugging
19

Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

Dec 29, 2015

Download

Documents

Kristian Jordan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

lec 04

Content Providers

Adapters

CursorLoaders

Advanced Debugging

Page 2: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

Preferences: key/value pairs (we will talk about these next lecture)

SQLite: Use a very small RDBMS that is sandboxed to your app and then expose it to other apps using a content provider

Files: You can read/write files to your raw directory-- avoid this.

Network: store data on the internet and read/write using any available Internet protocol – avoid this.

You can persist data in several ways :

Page 3: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

All first-class components in Android must be declared in the manifest file, they include:

ActivitiesServicesContentProvidersReceivers (aka BroadcastReceivers)

Page 4: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

http://developer.android.com/reference/android/provider/package-summary.html

These are the built-in Content Providers. You will notice things like:Contacts, Calendar, MediaStore, Albums, etc.

Existing Content Providers:

Page 5: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

Each of the .db's are sandboxed in their own application. These dbs can NOT be accessed outside their application context. To get or expose the data in the db, you must use a Content Provider

Page 6: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

CRUD: Create ReadUpdateDelete

Page 7: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

ContentValuesAndroid uses ContentValues as a shuttle to pass values into the ContentResolver.

Then, the ContentResolver to pass values into the ContentProvider, which in turn communicates to the db

ContentValues ContentResolver ContentProvider

db

Page 8: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

content://edu.uchicago.cs.gerber.restos/restos/34

Anatomy of a URI for content provider

A B C D

A: the scheme – always contentB: the authorityC: optional pathD: individual id

Page 9: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

ADB (Android Dubug Bridge) adb kill-server adb start-server adb get-state adb devices

Use kill-server / start-server to reboot the adb if your device/emulator is not communicating with your dev-machine.

Page 10: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

Using Log.i Log.e Log.d Log.w Log.v and Log.wtf and use of filters

Advanced Debugging

Page 11: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

Exploring the File Explorer /data/dataPush files to your phoneEmulator Control: voice/sms messagesProfiler using a slow() method.

Advanced Debugging

Page 12: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

HeirarchyViewerMust be on the emulator and runningIn [c:\java\]android-sdk\tools\hierarchyviewerhttp://www.youtube.com/watch?v=PAgE7saQUUY (good video)Measure/Layout/Draw

Advanced Debugging

Page 13: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

2 benefits of CursorLoaders:

The query is handled on a background thread for you (courtesy of being build on AsyncTaskLoader) so large data queries do not block the UI. This is something the docs recommended you do for yourself when using a plain Cursor, but now it's done under the hood.

CursorLoader is auto-updating. In addition to performing the initial query, the CursorLoader registers a ContentObserver with the dataset you requested and calls forceLoad() on itself when the data set changes. This results in you getting async callbacks anytime the data changes in order to update the view.

http://stackoverflow.com/questions/7182920/what-are-the-benefits-of-cursorloaders

CursorLoaders

Page 14: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

http://stackoverflow.com/questions/7182920/what-are-the-benefits-of-cursorloaders

CursorLoaders

Calling initLoader() will result in the onCreateLoader() method where you will construct the query and a new CursorLoader instance, if necessary.

The onLoadFinished() method will be fired each time new data is available, and will include the latest Cursor for you to attach to the view or otherwise iterate through.

You will notice that the reference to the cursor in the Adapter is null, this is because the CursorLoader is now managing the cursor.

Page 15: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

Adapters

An adapter needs to know the following in its constructor//the context in which it will operate (required)//the source data (required)//the list item layout (required)//the target fields -- if more than one. //cursor – if this is a cursorAdapter

//you must then set the adapter to the Target View

Page 16: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

AdapterViews

The bottom line are all appropriate Target Views

Page 17: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

M

*LA PA

CP

A

Midterm min requirements:

ListActivity that is the main/launcher

New/View/Edit list item detail Activity

PreferencesActitivity for prefs

ActionBar on ListActivity

You may exceed the minimum requirements.

Midterm Assignment – you may use the base code or you may make your own.

Page 18: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

A: AsyncTask: How to get/parse JSON data (weather data) from RESTful web service (with recipe)

B: MediaStore: Taking pictures and videos and storing them for use in an app (not the standard gallery) (with recipe)

C: Voice Recognition: How to use voice recognition inside an app (with recipe)

D: Tracking locations (using GPS) for a jog or a race. Data capture so that I can map where I went and how far I traveled, avg speed (with recipe)

Student presentations:

Page 19: Lec 04 Content Providers Adapters CursorLoaders Advanced Debugging.

M

*LA PA

CP

A

Midterm min requirements:

ListActivity that is the main/launcher

New/View/Edit list item detail Activity

PreferencesActitivity for prefs

ActionBar on ListActivity

You may exceed the minimum requirements.

Midterm Assignment – you may use the base code or you may make your own.