From c88f13e0cbf16793009a7e8e62b6517c835ed6cb Mon Sep 17 00:00:00 2001 From: Tom O'Neill Date: Thu, 17 Dec 2009 12:24:05 -0800 Subject: Update Notepad tutorial documentation to match code Some recent CLs to the Notepad source files fixed a couple of bugs in the code: http://b/issue?id=2266994 http://b/issue?id=2266962 This CL includes the newly corrected code in the zip file that developers download. The zip file was generated as follows: cd development/tutorials zip -r /tmp/NotepadCodeLab NotepadCodeLab -x \*Android.mk This CL also updates the documentation to match the new version of the code. Thus the code the users generate will include the bug fix. --- .../tutorials/notepad/codelab/NotepadCodeLab.zip | Bin 90916 -> 88600 bytes .../resources/tutorials/notepad/notepad-ex2.jd | 110 ++++++++++----------- .../resources/tutorials/notepad/notepad-ex3.jd | 83 +++++++++------- 3 files changed, 101 insertions(+), 92 deletions(-) (limited to 'docs/html/resources') diff --git a/docs/html/resources/tutorials/notepad/codelab/NotepadCodeLab.zip b/docs/html/resources/tutorials/notepad/codelab/NotepadCodeLab.zip index 24fefc1..502a326 100644 Binary files a/docs/html/resources/tutorials/notepad/codelab/NotepadCodeLab.zip and b/docs/html/resources/tutorials/notepad/codelab/NotepadCodeLab.zip differ diff --git a/docs/html/resources/tutorials/notepad/notepad-ex2.jd b/docs/html/resources/tutorials/notepad/notepad-ex2.jd index bab9471..44a04ab 100644 --- a/docs/html/resources/tutorials/notepad/notepad-ex2.jd +++ b/docs/html/resources/tutorials/notepad/notepad-ex2.jd @@ -5,7 +5,7 @@ parent.link=index.html

In this exercise, you will add a second Activity to your notepad application, to let the user -create and edit notes. You will also allow the user to delete existing notes through a context menu. +create and edit notes. You will also allow the user to delete existing notes through a context menu. The new Activity assumes responsibility for creating new notes by collecting user input and packing it into a return Bundle provided by the intent. This exercise demonstrates:

@@ -55,7 +55,7 @@ Tools > Fix Project Properties.

  • There are also a couple of new overridden methods - (onCreateContextMenu(), onContextItemSelected(), + (onCreateContextMenu(), onContextItemSelected(), onListItemClick() and onActivityResult()) which we will be filling in below.
  • @@ -75,7 +75,7 @@ Open the Notepadv2 class.

    1. In order for each list item in the ListView to register for the context menu, we call - registerForContextMenu() and pass it our ListView. So, at the very end of + registerForContextMenu() and pass it our ListView. So, at the very end of the onCreate() method add this line:
      registerForContextMenu(getListView());

      Because our Activity extends the ListActivity class, getListView() will return us @@ -91,7 +91,7 @@ public boolean onCreateContextMenu(Menu menu, View v super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, DELETE_ID, 0, R.string.menu_delete); } -

      The onCreateContextMenu() callback some passes other information in addition to the Menu object, +

      The onCreateContextMenu() callback passes some other information in addition to the Menu object, such as the View that has been triggered for the menu and an extra object that may contain additional information about the object selected. However, we don't care about these here, because we only have one kind of object in the Activity that uses context menus. In the next @@ -102,7 +102,7 @@ public boolean onCreateContextMenu(Menu menu, View v

      Step 3

      Now that the we've registered our ListView for a context menu and defined our context menu item, we need to handle the callback when it is selected. For this, we need to identify the list ID of the - selected item, then delete it. So fill in the + selected item, then delete it. So fill in the onContextItemSelected() method like this:

       public boolean onContextItemSelected(MenuItem item) {
      @@ -127,15 +127,15 @@ can now be deleted.

      margin-bottom:.5em;margin-top:1em;padding:0em;width:240px;">

      Starting Other Activities

      -

      In this example our Intent uses a class name specifically. - As well as - starting intents in - classes we already know about, be they in our own application or another - application, we can also create Intents without knowing exactly which + As well as + starting intents in + classes we already know about, be they in our own application or another + application, we can also create Intents without knowing exactly which application will handle it.

      -

      For example, we might want to open a page in a +

      For example, we might want to open a page in a browser, and for this we still use an Intent. But instead of specifying a class to handle it, we use a predefined Intent constant, and a content URI that describes what we @@ -155,7 +155,7 @@ startActivityForResult(i, ACTIVITY_CREATE);

      NoteEdit. Since the Intent class will need to communicate with the Android operating system to route requests, we also have to provide a Context (this).

      The startActivityForResult() method fires the Intent in a way that causes a method - in our Activity to be called when the new Activity is completed. The method in our Activity + in our Activity to be called when the new Activity is completed. The method in our Activity that receives the callback is called onActivityResult() and we will implement it in a later step. The other way to call an Activity is using startActivity() but this is a "fire-and-forget" way @@ -164,7 +164,7 @@ startActivityForResult(i, ACTIVITY_CREATE);

      Don't worry about the fact that NoteEdit doesn't exist yet, we will fix that soon.

    2. - +

      Step 5

      @@ -180,7 +180,7 @@ startActivityForResult(i, ACTIVITY_CREATE); interested in is the position that the user selected. We use this to get the data from the correct row, and bundle it up to send to the NoteEdit Activity.

      -

      In our implementation of the callback, the method creates an +

      In our implementation of the callback, the method creates an Intent to edit the note using the NoteEdit class. It then adds data into the extras Bundle of the Intent, which will be passed to the called Activity. We use it @@ -207,7 +207,7 @@ startActivityForResult(i, ACTIVITY_EDIT);

    3. The details of the note are pulled out from our query Cursor, which we move to the - proper position for the element that was selected in the list, with + proper position for the element that was selected in the list, with the moveToPosition() method.
    4. With the extras added to the Intent, we invoke the Intent on the NoteEdit class by passing startActivityForResult() @@ -219,7 +219,7 @@ startActivityForResult(i, ACTIVITY_EDIT); variable is much more efficient than accessing a field in the Dalvik VM, so by doing this we make only one access to the field, and five accesses to the local variable, making the routine much more efficient. It is recommended that you use this optimization when possible.

      - +

      Step 6

      @@ -243,7 +243,7 @@ startActivityForResult(i, ACTIVITY_EDIT);
    5. intent — this is an Intent created by the Activity returning results. It can be used to return data in the Intent "extras."
    6. - +

      The combination of startActivityForResult() and onActivityResult() can be thought of as an asynchronous RPC (remote procedure call) and forms the recommended way for an Activity to invoke @@ -277,7 +277,7 @@ case ACTIVITY_EDIT: ACTIVITY_EDIT activity results in this method.

    7. - In the case of a create, we pull the title and body from the extras (retrieved from the + In the case of a create, we pull the title and body from the extras (retrieved from the returned Intent) and use them to create a new note.
    8. @@ -288,7 +288,7 @@ case ACTIVITY_EDIT: fillData() at the end ensures everything is up to date .
    9. - +

      Step 7

      @@ -305,8 +305,8 @@ case ACTIVITY_EDIT: good UI is part art and part science, and the rest is work. Mastery of Declaring Layout is an essential part of creating a good looking Android application.

      -

      Take a look at the +

      Take a look at the Hello Views for some example layouts and how to use them. The ApiDemos sample project is also a great resource from which to learn how to create different layouts.

      @@ -335,7 +335,7 @@ case ACTIVITY_EDIT: layout_weight specified, so it takes up the minimum space required to render. If the layout_weight of each of the two text edit elements is set to 1, the remaining width in the parent layout will - be split equally between them (because we claim they are equally important). + be split equally between them (because we claim they are equally important). If the first one has a layout_weight of 1 and the second has a layout_weight of 2, then one third of the remaining space will be given to the first, and two thirds to the @@ -373,7 +373,7 @@ case ACTIVITY_EDIT: onCreate(Bundle) — and check the box next to it.
    10. Click OK.

      The method should now appear in your class.

    - +

    Step 9

    Fill in the body of the onCreate() method for NoteEdit.

    @@ -388,7 +388,7 @@ case ACTIVITY_EDIT:

    We can then unbundle the values that were passed in to the Activity with the extras Bundle attached to the calling Intent. We'll use them to pre-populate the title and body text edit views so that the user can edit them. - Then we will grab and store the mRowId so we can keep + Then we will grab and store the mRowId so we can keep track of what note the user is editing.

      @@ -406,14 +406,14 @@ case ACTIVITY_EDIT: mTitleText = (EditText) findViewById(R.id.title); mBodyText = (EditText) findViewById(R.id.body); Button confirmButton = (Button) findViewById(R.id.confirm); -

      Note that mTitleText and mBodyText are member +

      Note that mTitleText and mBodyText are member fields (you need to declare them at the top of the class definition).

    1. At the top of the class, declare a Long mRowId private field to store the current mRowId being edited (if any).
    2. -
    3. Continuing inside onCreate(), - add code to initialize the title, body and +
    4. Continuing inside onCreate(), + add code to initialize the title, body and mRowId from the extras Bundle in the Intent (if it is present):
      @@ -423,7 +423,7 @@ if (extras != null) {
           String title = extras.getString(NotesDbAdapter.KEY_TITLE);
           String body = extras.getString(NotesDbAdapter.KEY_BODY);
           mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
      -           
      +
           if (title != null) {
               mTitleText.setText(title);
           }
      @@ -459,16 +459,16 @@ if (extras != null) {
       confirmButton.setOnClickListener(new View.OnClickListener() {
       
           public void onClick(View view) {
      -               
      +
           }
      -           
      +
       });

    Step 10

    Fill in the body of the onClick() method of the OnClickListener created in the last step.

    - +

    This is the code that will be run when the user clicks on the confirm button. We want this to grab the title and body text from the edit text fields, and put them into the return Bundle so that they can be passed @@ -483,7 +483,7 @@ confirmButton.setOnClickListener(new View.OnClickListener() { constants defined in Notepadv2 as keys:

     Bundle bundle = new Bundle();
    -      
    +
     bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText().toString());
     bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
     if (mRowId != null) {
    @@ -498,7 +498,7 @@ mIntent.putExtras(bundle);
     setResult(RESULT_OK, mIntent);
     finish();