Bài giảng Lập trình cho thiết bị di động - Bài 3: Xử lý tập tin, lưu trạng thái ứng dụng, thao tác cơ sở dữ liệu và content provider

1. Files

2. XML Parser

3. Shared Preferences

4. SQLite

5. Content Provider

 

pptx97 trang | Chuyên mục: Android | Chia sẻ: dkS00TYs | Lượt xem: 2039 | Lượt tải: 4download
Tóm tắt nội dung Bài giảng Lập trình cho thiết bị di động - Bài 3: Xử lý tập tin, lưu trạng thái ứng dụng, thao tác cơ sở dữ liệu và content provider, để xem tài liệu hoàn chỉnh bạn click vào nút "TẢI VỀ" ở trên
ialog. ➤ ListPreference The preference equivalent of a spinner. Selecting this preference will display a dialog box containing a list of values from which to select. You can specify different arrays to contain the display text and selection values. ➤ RingtonePreference A specialized List Preference that presents the list of available ringtones for user selection. This is particularly useful when you’re constructing a screen to configure notification settings. 51 3.4 Activity and Framework Preferences 2. Create Activity for Preference Layout Extends from PreferenceActivity class 3. Config manifest xml 52 3.4 Activity and Framework Preferences 4. Modify MainActivity 53 3.4 Activity and Framework Preferences MainActivity SettingActivity SettingActivity Information get from MainActivity 54 4. SQLite 4.1 Introduction 4.2 Creating Database 4.3 Creating Table 4.4 Action Query:Insert, Update, Delete 4.5 Querying SQLite 4.6 Simple Database Demo 55 4.1 Introduction Embedded standalone program called sqlite3 1.SQLite implements most of the SQL-92 standard for SQL. 2.support for triggers and allows most complex queries (exception made for outer joins). 3.SQLITE does not implement referential integrity constraints through the foreign key constraint model. 4.SQLite uses a relaxed data typing model. 5.Instead of assigning a type to an entire column, types are assigned to individual values. This is similar to the Variant type in Visual Basic. 6.Therefore it is possible to insert a string into numeric column and so on. 56 4.2 Creating Database The simplest way to create a new SQLiteDatabase instance for your application is to use the openOrCreateDatabase() method of your application Context import android.database.sqlite.SQLiteDatabase; 57 4.2 Creating Database Storing Database directory: /data/data/app/databases/ So, in this case, the path to the database would be: 58 4.2 Creating Database Beware of sharing issues. You cannot access internal databases belonging to other people (instead use Content Providers or external SD resident DBs). An SD resident database requires the Manifest to include: 59 4.3 Creating Table 60 4.4 Action Query:Insert, Update, Delete Inserting Records: We use the insert() method to add new data to our tables. We use the ContentValues object to pair the column names to the column values for the record we want to insert. 61 4.4 Action Query:Insert, Update, Delete Updating Records: You can modify records in the database using the update() method.The update() method takes four arguments: The table to update records A ContentValues object with the modified fields to update An optional WHERE clause, in which ? identifies a WHERE clause argument An array of WHERE clause arguments, each of which is substituted in place of the ?s from the second parameter public int update (String table, ContentValues values, 	 String whereClause, String[] whereArgs) This method Returns the number of rows affected 62 4.4 Action Query:Insert, Update, Delete Updating Records: Because we are not updating the other fields, we do not need to include them in the ContentValues object.We include only the tenlop field because it is the only field we change. 63 4.4 Action Query:Insert, Update, Delete Deleting Records: You can remove records from the database using the remove() method.The remove() method takes 3 arguments: The table to delete the record from An optional WHERE clause, in which ? identifies a WHERE clause argument An array of WHERE clause arguments, each of which is substituted in place of the ?s from the second parameter Passing null to the WHERE clause deletes all records in the table. public int delete (String table, 	 String whereClause, 	 String[] whereArgs) This method Returns the number of rows affected 64 4.4 Action Query:Insert, Update, Delete Deleting Records: Delete all rows in table tblop: Delete row with malop=“dhth7c”: delete method will return the number of rows affected 65 4.5 Querying SQLite When results are returned from a SQL query, you often access them using a Cursor found in the android.database.Cursor class. Cursor objects are like file pointers; they allow random access to query results. public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy) Returns A Cursor object, which is positioned before the first entry. Note that Cursors are not synchronized 66 4.5 Querying SQLite table The table name to compile the query against. columns A list of which columns to return. Passing null will return all columns, which is discouraged to prevent reading data from storage that isn't going to be used. selection A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given table. selectionArgs You may include ?s in selection, which will be replaced by the values from selectionArgs, in order that they appear in the selection. The values will be bound as Strings. groupBy A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself). Passing null will cause the rows to not be grouped. having A filter declare which row groups to include in the cursor, if row grouping is being used, formatted as an SQL HAVING clause (excluding the HAVING itself). Passing null will cause all row groups to be included, and is required when row grouping is not being used. orderBy How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself). Passing null will use the default sort order, which may be unordered. 67 4.5 Querying SQLite 68 4.6 Simple Database Demo Transactiontake yourself SQLiteOpenHelpertake yourself 69 5. Content Provider 5.1 Introduction 5.2 Using common Content Provider 5.3 Create your own Content Provider 70 5.1 Introduction content provider is a specialized type of data store that exposes standardized ways to retrieve and manipulate the stored data. You wish to share data between applications, you need to use the content provider model as recommended in Android. most useful built-in content providers 71 5.2 Using common Content Provider To query a content provider, you provide a query string in the form of a URI, with an optional specifier for a particular row, using the following syntax: ://// For example, to retrieve all the bookmarks stored by your web browsers (in Android), you would use the following content URI: content://browser/bookmarks To retrieve all the contacts stored by the Contacts application, the URI would look like this: content://contacts/people To retrieve a particular contact, you can specify the URI with a specific ID: content://contacts/people/3 72 5.2 Using common Content Provider Retrieves a managed cursor CursorLoader loader=new 	CursorLoader(context, uri, null, null, null, null); Cursor c=loader.loadInBackground(); Is equivalent to: Cursor c = getContentResolver() 	.query(uri, null, null, null, null); getContentResolver() method returns a ContentResolver object, which helps to resolve a content URI with the appropriate content provider Parametres: URI, projection, SQLWHERE, ORDERBY 73 5.2 Using common Content Provider Get contacts: 74 5.2 Using common Content Provider Get contacts: Or using getContentResolver instead CursorLoader: 75 5.2 Using common Content Provider Access Call Log: 76 5.2 Using common Content Provider Access Call Log: Similar the Contact, you could use CursorLoader class to access the call log 77 5.2 Using common Content Provider Access Media Store: 78 5.2 Using common Content Provider Access Media Store: Similar another provider, you could use getContentResolver to access the Media 79 5.2 Using common Content Provider Access Book mark: 80 5.2 Using common Content Provider Access Book mark: 81 5.3 Create your own Content Provider extend the abstract ContentProvider class and override the various methods defined within it. The 6 methods you need to implement are: getType(): Returns the MIME type of the data at the given URI. onCreate(): Called when the provider is being started. query(): Receives a request from a client. The result is returned as a Cursor object. insert(): Inserts a new record into the content provider. delete(): Deletes an existing record from the content provider. update(): Updates an existing record from the content provider. Within your content provider, you may choose to store your data however you like: traditional file system, XML, database, or even through web services 82 5.3 Create your own Content Provider Declare Provider in Manifest file: Declare Content URI and Path: Example public static final String AUTHORITY = "abc.com.MyProvider"; 	// Same as Androidmanifest.xml entry public static final String CONTENT_URI = 	"content://"+ AUTHORITY + "/demodb"); 	//URI for access the Content Provider Using UriMatcher: The UriMatcher class is a helper class for pattern matching on the URIs that are passed to this content provider. Declare the Constant Content Provider Values: 83 5.3 Create your own Content Provider Create a Project with name “LearnCreateOwnContentProvider” Example to understand this technical 84 5.3 Create your own Content Provider DatabaseHelper class 85 5.3 Create your own Content Provider Movie class 86 5.3 Create your own Content Provider Movie class 87 5.3 Create your own Content Provider Movie class 88 5.3 Create your own Content Provider MyContentProvider class 89 5.3 Create your own Content Provider MyContentProvider class 90 5.3 Create your own Content Provider MyContentProvider class 91 5.3 Create your own Content Provider MainActivity XML 92 5.3 Create your own Content Provider MainActivity class 93 5.3 Create your own Content Provider MainActivity class 94 5.3 Create your own Content Provider MainActivity class 95 5.3 Create your own Content Provider MainActivity class 96 5.3 Create your own Content Provider MainActivity class 97 5.3 Create your own Content Provider Manifest XML END 98 

File đính kèm:

  • pptx3 - XỬ LÝ TẬP TIN_ LƯU TRẠNG THÁI ỨNG DỤNG_ THAO TÁC CƠ SỞ DỮ LIỆU VÀ CONTENT PROVIDER.pptx
Tài liệu liên quan