• Aucun résultat trouvé

Update a Database

Dans le document 2. Building Your First App (Page 93-96)

SQLiteDatabase db = mDbHelper.getReadableDatabase();

// Define a projection that specifies which columns from the database // you will actually use after this query.

String[] projection = { FeedEntry._ID,

FeedEntry.COLUMN_NAME_TITLE, FeedEntry.COLUMN_NAME_UPDATED, ...

};

// How you want the results sorted in the resulting Cursor String sortOrder =

FeedEntry.COLUMN_NAME_UPDATED + " DESC";

Cursor c = db.query(

FeedEntry.TABLE_NAME, // The table to query

projection, // The columns to return

selection, // The columns for the WHERE clause selectionArgs, // The values for the WHERE clause null, // don't group the rows

null, // don't filter by row groups sortOrder // The sort order

);

To look at a row in the cursor, use one of the Cursor move methods, which you must always call before you begin reading values. Generally, you should start by calling moveToFirst(), which places the "read position" on the first entry in the results. For each row, you can read a column's value by calling one of the Cursor get methods, such as getString() or getLong(). For each of the get methods, you must pass the index position of the column you desire, which you can get by calling getColumnIndex() or

getColumnIndexOrThrow(). For example:

cursor.moveToFirst();

long itemId = cursor.getLong(

cursor.getColumnIndexOrThrow(FeedEntry._ID) );

Delete Information from a Database

To delete rows from a table, you need to provide selection criteria that identify the rows. The database API provides a mechanism for creating selection criteria that protects against SQL injection. The mechanism divides the selection specification into a selection clause and selection arguments. The clause defines the columns to look at, and also allows you to combine column tests. The arguments are values to test against that are bound into the clause. Because the result isn't handled the same as a regular SQL statement, it is immune to SQL injection.

// Define 'where' part of query.

String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";

// Specify arguments in placeholder order.

String[] selectionArgs = { String.valueOf(rowId) };

// Issue SQL statement.

db.delete(table_name, selection, selectionArgs);

Update a Database

When you need to modify a subset of your database values, use the update() method.

Saving Data in SQL Databases

94

Content from developer.android.com/training/basics/data-storage/databases.html through their Creative Commons Attribution 2.5 license

Updating the table combines the content values syntax of insert() with the where syntax of delete().

SQLiteDatabase db = mDbHelper.getReadableDatabase();

// New value for one column

ContentValues values = new ContentValues();

values.put(FeedEntry.COLUMN_NAME_TITLE, title);

// Which row to update, based on the ID

String selection = FeedEntry.COLUMN_NAME_ENTRY_ID + " LIKE ?";

String[] selectionArgs = { String.valueOf(rowId) };

int count = db.update(

FeedReaderDbHelper.FeedEntry.TABLE_NAME, values,

selection, selectionArgs);

Interacting with Other Apps

95

Content from developer.android.com/training/basics/intents/index.html through their Creative Commons Attribution 2.5 license

29. Interacting with Other Apps

Content from developer.android.com/training/basics/intents/index.html through their Creative Commons Attribution 2.5 license

An Android app typically has several activities.

Each activity displays a user interface that allows the user to perform a specific task (such as view a map or take a photo). To take the user from one activity to another, your app must use an Intent to define your app's "intent" to do something.

When you pass an Intent to the system with a method such as startActivity(), the system uses the Intent to identify and start the

appropriate app component. Using intents even allows your app to start an activity that is contained in a separate app.

An Intent can be explicit in order to start a specific component (a specific Activity instance) or implicit in order to start any component that can handle the intended action (such as "capture a photo").

This class shows you how to use an Intent to perform some basic interactions with other apps, such as start another app, receive a result from that app, and make your app able to respond to intents from other apps.

Lessons

Sending the User to Another App

Shows how you can create implicit intents to launch other apps that can perform an action.

Getting a Result from an Activity

Shows how to start another activity and receive a result from the activity.

Allowing Other Apps to Start Your Activity

Shows how to make activities in your app open for use by other apps by defining intent filters that declare the implicit intents your app accepts.

Dependencies and prerequisites

• Basic understanding of the Activity lifecycle (see Managing the Activity Lifecycle)

You should also read

• Sharing Simple Data

• Sharing Files

• Integrating Application with Intents (blog post)

• Intents and Intent Filters

Sending the User to Another App

96

Content from developer.android.com/training/basics/intents/sending.html through their Creative Commons Attribution 2.5 license

30. Sending the User to Another App

Content from developer.android.com/training/basics/intents/sending.html through their Creative Commons Attribution 2.5 license

One of Android's most important features is an app's ability to send the user to another app based on an "action" it would like to perform. For example, if your app has the address of a business that you'd like to show on a map, you don't have to build an activity in your app that shows a map. Instead, you can create a request to view the address using an Intent. The Android system then starts an app that's able to show the address on a map.

As explained in the first class, Building Your First App, you must use intents to navigate between activities in your own app. You generally do so

with an explicit intent, which defines the exact class name of the component you want to start. However, when you want to have a separate app perform an action, such as "view a map," you must use an implicit intent.

This lesson shows you how to create an implicit intent for a particular action, and how to use it to start an activity that performs the action in another app.

Dans le document 2. Building Your First App (Page 93-96)