Download presentation
Presentation is loading. Please wait.
Published byEunice Riley Modified over 9 years ago
1
Content providers Accessing shared data in a uniform way 1Content providers
2
What are content providers? Content providers is the recommended way to share data across packages Content provider = data store – Uniform interface to the data store – The physical storage is not relevant It’s a ”private” matter Example, standard content providers – Browser: bookmarks, browser history, etc. – CallLog: Phone calls, etc. – Contacts: Names, addresses, phone numbers, etc. – Setting: The device’s settings / preferences 2Content providers
3
How to read content? Uri u = Uri.parse(”content://contacts/people”) – General (XPath like) syntax Source://element/sub-element Cursor c = managedQuery(u, projection, selection, selectionArgs, sortOrder) – Similar to a SQL Select statement Select projection from table where selection = selectionArgs order by sortOrder Example: ContentProvidersUsing – AndroidManifest.xml: Permission 3Content providers
4
Cursor, some methods boolean cursor.moveToFirst() int cursor.getColumnIndex(columnName) int cursor.getInt(columnIndex) – Similar methods for other data types – String cursor.getString(columnIndex) boolean cursor.moveToNext() cursor.close() 4Content providers
5
CursorAdapter – General adapter class to expose data to a ListView SimpleCursorAdapter – Subclass of CursorAdapter – Simple adapter class to map columns of a cursor to TextViews or ImageViews defined in an layout XML file – Constructor: public SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)ContextCursorString[] Context: the ListActivity Layout: ListView from layout XML file C: The cursor referencing the data From: Array of columns from the cursor data, to be shown in the List To: The views that should show the data. Defined in the layout XML file – Source developer.android.com/reference/android/widget/SimpleCursorAdapter. html Content providers5
6
Creating your own content providers You can create you own content providers – Uniform access to the data Wrap – Files, SQLite database, network connections, etc. Requires quite a lot of code. 6Content providers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.