1 page.title=Notepad Exercise 1 2 parent.title=Notepad Tutorial 3 parent.link=index.html 4 @jd:body 5 6 7 <p><em>In this exercise, you will construct a simple notes list that lets the 8 user add new notes but not edit them. The exercise demonstrates:</em></p> 9 <ul> 10 <li><em>The basics of <code>ListActivities</code> and creating and handling menu 11 options. </em></li> 12 <li><em>How to use a SQLite database to store the notes.</em></li> 13 <li><em>How to bind data from a database cursor into a ListView using a 14 SimpleCursorAdapter.</em></li> 15 <li><em>The basics of screen layouts, including how to lay out a list view, how 16 you can add items to the activity menu, and how the activity handles those menu 17 selections. </em></li> 18 </ul> 19 20 <div style="float:right;white-space:nowrap"> 21 <span style="color:#BBB;"> 22 [<a href="notepad-ex1.html" style="color:#BBB;">Exercise 1</a>]</span> 23 [<a href="notepad-ex2.html">Exercise 2</a>] 24 [<a href="notepad-ex3.html">Exercise 3</a>] 25 [<a href="notepad-extra-credit.html">Extra Credit</a>] 26 </div> 27 28 29 30 <h2>Step 1</h2> 31 32 <p>Open up the <code>Notepadv1</code> project in Eclipse.</p> 33 34 <p><code>Notepadv1</code> is a project that is provided as a starting point. It 35 takes care of some of the boilerplate work that you have already seen if you 36 followed the <a href="{@docRoot}resources/tutorials/hello-world.html">Hello, 37 World</a> tutorial.</p> 38 39 <ol> 40 <li> 41 Start a new Android Project by clicking <strong>File</strong> > 42 <strong>New</strong> > <strong>Android Project</strong>.</li> 43 <li> 44 In the New Android Project dialog, select <strong>Create project from existing source</strong>.</li> 45 <li> 46 Click <strong>Browse</strong> and navigate to where you copied the <code>NotepadCodeLab</code> 47 (downloaded during <a href="{@docRoot}resources/tutorials/notepad/index.html#preparing">setup</a>) 48 and select <code>Notepadv1</code>.</li> 49 <li> 50 The Project Name and other properties should be automatically filled for you. 51 You must select the Build Target—we recommend selecting a target with the 52 lowest platform version available. Also add an integer to the Min SDK Version field 53 that matches the API Level of the selected Build Target.</li> 54 <li> 55 Click <strong>Finish</strong>. The <code>Notepadv1</code> project should open and be 56 visible in your Eclipse package explorer.</li> 57 </ol> 58 59 <p>If you see an error about <code>AndroidManifest.xml</code>, or some 60 problems related to an Android zip file, right click on the project and 61 select <strong>Android Tools</strong> > <strong>Fix Project Properties</strong>. 62 (The project is looking in the wrong location for the library file, 63 this will fix it for you.)</p> 64 65 <h2>Step 2</h2> 66 67 <div class="sidebox-wrapper"> 68 <div class="sidebox"> 69 <h2>Accessing and modifying data</h2> 70 <p>For this 71 exercise, we are using a SQLite database to store our data. This is useful 72 if only <em>your</em> application will need to access or modify the data. If you wish for 73 other activities to access or modify the data, you have to expose the data using a 74 {@link android.content.ContentProvider ContentProvider}.</p> 75 <p>If you are interested, you can find out more about 76 <a href="{@docRoot}guide/topics/providers/content-providers.html">content providers</a> or the 77 whole 78 subject of <a href="{@docRoot}guide/topics/data/data-storage.html">Data Storage</a>. 79 The NotePad sample in the <code>samples/</code> folder of the SDK also has an example of how 80 to create a ContentProvider.</p> 81 </div> 82 </div> 83 84 <p>Take a look at the <code>NotesDbAdapter</code> class — this class is provided to 85 encapsulate data access to a SQLite database that will hold our notes data 86 and allow us to update it.</p> 87 <p>At the top of the class are some constant definitions that will be used in the application 88 to look up data from the proper field names in the database. There is also a database creation 89 string defined, which is used to create a new database schema if one doesn't exist already.</p> 90 <p>Our database will have the name <code>data</code>, and have a single table called 91 <code>notes</code>, which in turn has three fields: <code>_id</code>, <code>title</code> and 92 <code>body</code>. The <code>_id</code> is named with an underscore convention used in a number of 93 places inside the Android SDK and helps keep a track of state. The <code>_id</code> 94 usually has to be specified when querying or updating the database (in the column projections 95 and so on). The other two fields are simple text fields that will store data. 96 </p> 97 <p>The constructor for <code>NotesDbAdapter</code> takes a Context, which allows it to communicate with aspects 98 of the Android operating system. This is quite common for classes that need to touch the 99 Android system in some way. The Activity class implements the Context class, so usually you will just pass 100 <code>this</code> from your Activity, when needing a Context.</p> 101 <p>The <code>open()</code> method calls up an instance of DatabaseHelper, which is our local 102 implementation of the SQLiteOpenHelper class. It calls <code>getWritableDatabase()</code>, 103 which handles creating/opening a database for us.</p> 104 <p><code>close()</code> just closes the database, releasing resources related to the 105 connection.</p> 106 <p><code>createNote()</code> takes strings for the title and body of a new note, 107 then creates that note in the database. Assuming the new note is created successfully, the 108 method also returns the row <code>_id</code> value for the newly created note.</p> 109 <p><code>deleteNote()</code> takes a <var>rowId</var> for a particular note, and deletes that note from 110 the database.</p> 111 112 <p><code>fetchAllNotes()</code> issues a query to return a {@link android.database.Cursor} over all notes in the 113 database. The <code>query()</code> call is worth examination and understanding. The first field is the 114 name of the database table to query (in this case <code>DATABASE_TABLE</code> is "notes"). 115 The next is the list of columns we want returned, in this case we want the <code>_id</code>, 116 <code>title</code> and <code>body</code> columns so these are specified in the String array. 117 The remaining fields are, in order: <code>selection</code>, 118 <code>selectionArgs</code>, <code>groupBy</code>, <code>having</code> and <code>orderBy</code>. 119 Having these all <code>null</code> means we want all data, need no grouping, and will take the default 120 order. See {@link android.database.sqlite.SQLiteDatabase SQLiteDatabase} for more details.</p> 121 <p class="note"><b>Note:</b> A Cursor is returned rather than a collection of rows. This allows 122 Android to use resources efficiently -- instead of putting lots of data straight into memory 123 the cursor will retrieve and release data as it is needed, which is much more efficient for 124 tables with lots of rows.</p> 125 126 <p><code>fetchNote()</code> is similar to <code>fetchAllNotes()</code> but just gets one note 127 with the <var>rowId</var> we specify. It uses a slightly different version of the 128 {@link android.database.sqlite.SQLiteDatabase} <code>query()</code> method. 129 The first parameter (set <em>true</em>) indicates that we are interested 130 in one distinct result. The <var>selection</var> parameter (the fourth parameter) has been specified to search 131 only for the row "where _id =" the <var>rowId</var> we passed in. So we are returned a Cursor on 132 the one row.</p> 133 <p>And finally, <code>updateNote()</code> takes a <var>rowId</var>, <var>title</var> and <var>body</var>, and uses a 134 {@link android.content.ContentValues ContentValues} instance to update the note of the given 135 <var>rowId</var>.</p> 136 137 <h2 style="clear:right;">Step 3</h2> 138 139 <div class="sidebox-wrapper"> 140 <div class="sidebox"> 141 <h2>Layouts and activities</h2> 142 <p>Most Activity classes will have a layout associated with them. The layout 143 will be the "face" of the Activity to the user. In this case our layout will 144 take over the whole screen and provide a list of notes.</p> 145 <p>Full screen layouts are not the only option for an Activity however. You 146 might also want to use a <a 147 href="{@docRoot}resources/faq/commontasks.html#floatingorfull">floating 148 layout</a> (for example, a <a 149 href="{@docRoot}resources/faq/commontasks.html#dialogsandalerts">dialog 150 or alert</a>), 151 or perhaps you don't need a layout at all (the Activity will be invisible 152 to the user unless you specify some kind of layout for it to use).</p> 153 </div> 154 </div> 155 156 <p>Open the <code>notepad_list.xml</code> file in <code>res/layout</code> 157 and 158 take a look at it. (You may have to 159 hit the <em>xml</em> tab, at the bottom, in order to view the XML markup.)</p> 160 161 <p>This is a mostly-empty layout definition file. Here are some 162 things you should know about a layout file:</p> 163 164 165 <ul> 166 <li> 167 All Android layout files must start with the XML header line: 168 <code><?xml version="1.0" encoding="utf-8"?></code>. </li> 169 <li> 170 The next definition will often (but not always) be a layout 171 definition of some kind, in this case a <code>LinearLayout</code>. </li> 172 <li> 173 The XML namespace of Android should always be defined in 174 the top level component or layout in the XML so that <code>android:</code> tags can 175 be used through the rest of the file: 176 <p><code>xmlns:android="http://schemas.android.com/apk/res/android"</code></p> 177 </li> 178 </ul> 179 180 <h2 style="clear:right;">Step 4</h2> 181 <p>We need to create the layout to hold our list. Add code inside 182 of the <code>LinearLayout</code> element so the whole file looks like this: </p> 183 <pre> 184 <?xml version="1.0" encoding="utf-8"?> 185 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 186 android:layout_width="wrap_content" 187 android:layout_height="wrap_content"> 188 189 <ListView android:id="@android:id/list" 190 android:layout_width="wrap_content" 191 android:layout_height="wrap_content"/> 192 <TextView android:id="@android:id/empty" 193 android:layout_width="wrap_content" 194 android:layout_height="wrap_content" 195 android:text="@string/no_notes"/> 196 197 </LinearLayout> 198 </pre> 199 <ul> 200 <li> 201 The <strong>@</strong> symbol in the id strings of the <code>ListView</code> and 202 <code>TextView</code> tags means 203 that the XML parser should parse and expand the rest of 204 the id string and use an ID resource.</li> 205 <li> 206 The <code>ListView</code> and <code>TextView</code> can be 207 thought as two alternative views, only one of which will be displayed at once. 208 ListView will be used when there are notes to be shown, while the TextView 209 (which has a default value of "No Notes Yet!" defined as a string 210 resource in <code>res/values/strings.xml</code>) will be displayed if there 211 aren't any notes to display.</li> 212 <li>The <code>list</code> and <code>empty</code> IDs are 213 provided for us by the Android platform, so, we must 214 prefix the <code>id</code> with <code>android:</code> (e.g., <code>@android:id/list</code>).</li> 215 <li>The View with the <code>empty</code> id is used 216 automatically when the {@link android.widget.ListAdapter} has no data for the ListView. The 217 ListAdapter knows to look for this name by default. Alternatively, you could change the 218 default empty view by using {@link android.widget.AdapterView#setEmptyView(View)} 219 on the ListView. 220 <p> 221 More broadly, the <code>android.R</code> class is a set of predefined 222 resources provided for you by the platform, while your project's 223 <code>R</code> class is the set of resources your project has defined. 224 Resources found in the <code>android.R</code> resource class can be 225 used in the XML files by using the <code>android:</code> name space prefix 226 (as we see here).</p> 227 </li> 228 </ul> 229 230 <h2 style="clear:right;">Step 5</h2> 231 232 <div class="sidebox-wrapper"> 233 <div class="sidebox"> 234 <h2>Resources and the R class</h2> 235 <p>The folders under res/ in the Eclipse project are for resources. 236 There is a <a href="{@docRoot}resources/faq/commontasks.html#filelist">specific structure</a> 237 to the 238 folders and files under res/.</p> 239 <p>Resources defined in these folders and files will have 240 corresponding entries in the R class allowing them to be easily accessed 241 and used from your application. The R class is automatically generated using the contents 242 of the res/ folder by the eclipse plugin (or by aapt if you use the command line tools). 243 Furthermore, they will be bundled and deployed for you as part of the application.</p> 244 </p> 245 </div> 246 </div> 247 248 <p>To make the list of notes in the ListView, we also need to define a View for each row:</p> 249 <ol> 250 <li> 251 Create a new file under <code>res/layout</code> called 252 <code>notes_row.xml</code>. </li> 253 <li> 254 Add the following contents (note: again the XML header is used, and the 255 first node defines the Android XML namespace)<br> 256 <pre style="overflow:auto"> 257 <?xml version="1.0" encoding="utf-8"?> 258 <TextView android:id="@+id/text1" 259 xmlns:android="http://schemas.android.com/apk/res/android" 260 android:layout_width="wrap_content" 261 android:layout_height="wrap_content"/></pre> 262 <p> 263 This is the View that will be used for each notes title row — it has only 264 one text field in it. </p> 265 <p>In this case we create a new id called <code>text1</code>. The 266 <strong>+</strong> after the <strong>@</strong> in the id string indicates that the id should 267 be automatically created as a resource if it does not already exist, so we are defining 268 <code>text1</code> on the fly and then using it.</p> 269 </li> 270 <li>Save the file.</li> 271 </ol> 272 <p>Open the <code>R.java</code> class in the 273 project and look at it, you should see new definitions for 274 <code>notes_row</code> and <code>text1</code> (our new definitions) 275 meaning we can now gain access to these from the our code. </p> 276 277 <h2 style="clear:right;">Step 6</h2> 278 <p>Next, open the <code>Notepadv1</code> class in the source. In the following steps, we are going to 279 alter this class to become a list adapter and display our notes, and also 280 allow us to add new notes.</p> 281 282 <p><code>Notepadv1</code> will inherit from a subclass 283 of <code>Activity</code> called a <code>ListActivity</code>, 284 which has extra functionality to accommodate the kinds of 285 things you might want to do with a list, for 286 example: displaying an arbitrary number of list items in rows on the screen, 287 moving through the list items, and allowing them to be selected.</p> 288 289 <p>Take a look through the existing code in <code>Notepadv1</code> class. 290 There is a currently an unused private field called <code>mNoteNumber</code> that 291 we will use to create numbered note titles.</p> 292 <p>There are also three override methods defined: 293 <code>onCreate</code>, <code>onCreateOptionsMenu</code> and 294 <code>onOptionsItemSelected</code>; we need to fill these 295 out:</p> 296 <ul> 297 <li><code>onCreate()</code> is called when the activity is 298 started — it is a little like the "main" method for an Activity. We use 299 this to set up resources and state for the activity when it is 300 running.</li> 301 <li><code>onCreateOptionsMenu()</code> is used to populate the 302 menu for the Activity. This is shown when the user hits the menu button, 303 and 304 has a list of options they can select (like "Create 305 Note"). </li> 306 <li><code>onOptionsItemSelected()</code> is the other half of the 307 menu equation, it is used to handle events generated from the menu (e.g., 308 when the user selects the "Create Note" item). 309 </li> 310 </ul> 311 312 <h2>Step 7</h2> 313 <p>Change the inheritance of <code>Notepadv1</code> from 314 <code>Activity</code> 315 to <code>ListActivity</code>:</p> 316 <pre>public class Notepadv1 extends ListActivity</pre> 317 <p>Note: you will have to import <code>ListActivity</code> into the 318 Notepadv1 319 class using Eclipse, <strong>ctrl-shift-O</strong> on Windows or Linux, or 320 <strong>cmd-shift-O</strong> on the Mac (organize imports) will do this for you 321 after you've written the above change.</p> 322 323 <h2>Step 8</h2> 324 <p>Fill out the body of the <code>onCreate()</code> method.</p> 325 <p>Here we will set the title for the Activity (shown at the top of the 326 screen), use the <code>notepad_list</code> layout we created in XML, 327 set up the <code>NotesDbAdapter</code> instance that will 328 access notes data, and populate the list with the available note 329 titles:</p> 330 <ol> 331 <li> 332 In the <code>onCreate</code> method, call <code>super.onCreate()</code> with the 333 <code>savedInstanceState</code> parameter that's passed in.</li> 334 <li> 335 Call <code>setContentView()</code> and pass <code>R.layout.notepad_list</code>.</li> 336 <li> 337 At the top of the class, create a new private class field called <code>mDbHelper</code> of class 338 <code>NotesDbAdapter</code>. 339 </li> 340 <li> 341 Back in the <code>onCreate</code> method, construct a new 342 <code>NotesDbAdapter</code> 343 instance and assign it to the <code>mDbHelper</code> field (pass 344 <code>this</code> into the constructor for <code>DBHelper</code>) 345 </li> 346 <li> 347 Call the <code>open()</code> method on <code>mDbHelper</code> to open (or create) the 348 database. 349 </li> 350 <li> 351 Finally, call a new method <code>fillData()</code>, which will get the data and 352 populate the ListView using the helper — we haven't defined this method yet. </li> 353 </ol> 354 <p> 355 <code>onCreate()</code> should now look like this:</p> 356 <pre> 357 @Override 358 public void onCreate(Bundle savedInstanceState) { 359 super.onCreate(savedInstanceState); 360 setContentView(R.layout.notepad_list); 361 mDbHelper = new NotesDbAdapter(this); 362 mDbHelper.open(); 363 fillData(); 364 }</pre> 365 <p>And be sure you have the <code>mDbHelper</code> field definition (right 366 under the mNoteNumber definition): </p> 367 <pre> private NotesDbAdapter mDbHelper;</pre> 368 369 <h2>Step 9</h2> 370 371 <div class="sidebox-wrapper"> 372 <div class="sidebox"> 373 <h2>More about menus</h2> 374 <p>The notepad application we are constructing only scratches the 375 surface with <a href="{@docRoot}resources/faq/commontasks.html#addmenuitems">menus</a>. </p> 376 <p>You can also <a href="{@docRoot}resources/faq/commontasks.html#menukeyshortcuts">add 377 shortcut keys for menu items</a>, <a 378 href="{@docRoot}resources/faq/commontasks.html#menukeyshortcuts">create 379 submenus</a> and even <a href="{@docRoot}resources/faq/commontasks.html#addingtoothermenus">add 380 menu items to other applications!</a>. </p> 381 </div> 382 </div> 383 384 <p>Fill out the body of the <code>onCreateOptionsMenu()</code> method.</p> 385 386 <p>We will now create the "Add Item" button that can be accessed by pressing the menu 387 button on the device. We'll specify that it occupy the first position in the menu.</p> 388 389 <ol> 390 <li> 391 In <code>strings.xml</code> resource (under <code>res/values</code>), add 392 a new string named "menu_insert" with its value set to <code>Add Item</code>: 393 <pre><string name="menu_insert">Add Item</string></pre> 394 <p>Then save the file and return to <code>Notepadv1</code>.</p> 395 </li> 396 <li>Create a menu position constant at the top of the class: 397 <pre>public static final int INSERT_ID = Menu.FIRST;</pre> 398 </li> 399 <li>In the <code>onCreateOptionsMenu()</code> method, change the 400 <code>super</code> call so we capture the boolean return as <code>result</code>. We'll return this value at the end.</li> 401 <li>Then add the menu item with <code>menu.add()</code>.</li> 402 </ol> 403 <p>The whole method should now look like this: 404 <pre> 405 @Override 406 public boolean onCreateOptionsMenu(Menu menu) { 407 boolean result = super.onCreateOptionsMenu(menu); 408 menu.add(0, INSERT_ID, 0, R.string.menu_insert); 409 return result; 410 }</pre> 411 <p>The arguments passed to <code>add()</code> indicate: a group identifier for this menu (none, 412 in this case), a unique ID (defined above), the order of the item (zero indicates no preference), 413 and the resource of the string to use for the item.</p> 414 415 <h2 style="clear:right;">Step 10</h2> 416 <p>Fill out the body of the <code>onOptionsItemSelected()</code> method:</p> 417 <p>This is going 418 to handle our new "Add Note" menu item. When this is selected, the 419 <code>onOptionsItemSelected()</code> method will be called with the 420 <code>item.getId()</code> set to <code>INSERT_ID</code> (the constant we 421 used to identify the menu item). We can detect this, and take the 422 appropriate actions:</p> 423 <ol> 424 <li> 425 The <code>super.onOptionsItemSelected(item)</code> method call goes at the 426 end of this method — we want to catch our events first! </li> 427 <li> 428 Write a switch statement on <code>item.getItemId()</code>. 429 <p>In the case of <var>INSERT_ID</var>, call a new method, <code>createNote()</code>, 430 and return true, because we have handled this event and do not want to 431 propagate it through the system.</p> 432 </li> 433 <li>Return the result of the superclass' <code>onOptionsItemSelected()</code> 434 method at the end.</li> 435 </ol> 436 <p> 437 The whole <code>onOptionsItemSelect()</code> method should now look like 438 this:</p> 439 <pre> 440 @Override 441 public boolean onOptionsItemSelected(MenuItem item) { 442 switch (item.getItemId()) { 443 case INSERT_ID: 444 createNote(); 445 return true; 446 } 447 448 return super.onOptionsItemSelected(item); 449 }</pre> 450 451 <h2>Step 11</h2> 452 <p>Add a new <code>createNote()</code> method:</p> 453 <p>In this first version of 454 our application, <code>createNote()</code> is not going to be very useful. 455 We will simply 456 create a new note with a title assigned to it based on a counter ("Note 1", 457 "Note 2"...) and with an empty body. At present we have no way of editing 458 the contents of a note, so for now we will have to be content making one 459 with some default values:</p> 460 <ol> 461 <li>Construct the name using "Note" and the counter we defined in the class: <code> 462 String noteName = "Note " + mNoteNumber++</code></li> 463 <li> 464 Call <code>mDbHelper.createNote()</code> using <code>noteName</code> as the 465 title and <code>""</code> for the body 466 </li> 467 <li> 468 Call <code>fillData()</code> to populate the list of notes (inefficient but 469 simple) — we'll create this method next.</li> 470 </ol> 471 <p> 472 The whole <code>createNote()</code> method should look like this: </p> 473 <pre> 474 private void createNote() { 475 String noteName = "Note " + mNoteNumber++; 476 mDbHelper.createNote(noteName, ""); 477 fillData(); 478 }</pre> 479 480 481 <h2>Step 12</h2> 482 <div class="sidebox-wrapper"> 483 <div class="sidebox"> 484 <h2>List adapters</h2> 485 <p>Our example uses a {@link android.widget.SimpleCursorAdapter 486 SimpleCursorAdapter} to bind a database {@link android.database.Cursor Cursor} 487 into a ListView, and this is a common way to use a {@link android.widget.ListAdapter 488 ListAdapter}. Other options exist like {@link android.widget.ArrayAdapter ArrayAdapter} which 489 can be used to take a List or Array of in-memory data and bind it in to 490 a list as well.</p> 491 </div> 492 </div> 493 494 <p>Define the <code>fillData()</code> method:</p> 495 <p>This 496 method uses <code>SimpleCursorAdapter,</code> which takes a database <code>Cursor</code> 497 and binds it to fields provided in the layout. These fields define the row elements of our list 498 (in this case we use the <code>text1</code> field in our 499 <code>notes_row.xml</code> layout), so this allows us to easily populate the list with 500 entries from our database.</p> 501 <p>To do this we have to provide a mapping from the <code>title</code> field in the returned Cursor, to 502 our <code>text1</code> TextView, which is done by defining two arrays: the first a string array 503 with the list of columns to map <em>from</em> (just "title" in this case, from the constant 504 <code>NotesDbAdapter.KEY_TITLE</code>) and, the second, an int array 505 containing references to the views that we'll bind the data <em>into</em> 506 (the <code>R.id.text1</code> TextView).</p> 507 <p>This is a bigger chunk of code, so let's first take a look at it:</p> 508 509 <pre> 510 private void fillData() { 511 // Get all of the notes from the database and create the item list 512 Cursor c = mDbHelper.fetchAllNotes(); 513 startManagingCursor(c); 514 515 String[] from = new String[] { NotesDbAdapter.KEY_TITLE }; 516 int[] to = new int[] { R.id.text1 }; 517 518 // Now create an array adapter and set it to display using our row 519 SimpleCursorAdapter notes = 520 new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to); 521 setListAdapter(notes); 522 }</pre> 523 524 <p>Here's what we've done:</p> 525 <ol> 526 <li> 527 After obtaining the Cursor from <code>mDbHelper.fetchAllNotes()</code>, we 528 use an Activity method called 529 <code>startManagingCursor()</code> that allows Android to take care of the 530 Cursor lifecycle instead of us needing to worry about it. (We will cover the implications 531 of the lifecycle in exercise 3, but for now just know that this allows Android to do some 532 of our resource management work for us.)</li> 533 <li> 534 Then we create a string array in which we declare the column(s) we want 535 (just the title, in this case), and an int array that defines the View(s) 536 to which we'd like to bind the columns (these should be in order, respective to 537 the string array, but here we only have one for each).</li> 538 <li> 539 Next is the SimpleCursorAdapter instantiation. 540 Like many classes in Android, the SimpleCursorAdapter needs a Context in order to do its 541 work, so we pass in <code>this</code> for the context (since subclasses of Activity 542 implement Context). We pass the <code>notes_row</code> View we created as the receptacle 543 for the data, the Cursor we just created, and then our arrays.</li> 544 </ol> 545 <p> 546 In the future, remember that the mapping between the <strong>from</strong> columns and <strong>to</strong> resources 547 is done using the respective ordering of the two arrays. If we had more columns we wanted 548 to bind, and more Views to bind them in to, we would specify them in order, for example we 549 might use <code>{ NotesDbAdapter.KEY_TITLE, NotesDbAdapter.KEY_BODY }</code> and 550 <code>{ R.id.text1, R.id.text2 }</code> to bind two fields into the row (and we would also need 551 to define text2 in the notes_row.xml, for the body text). This is how you can bind multiple fields 552 into a single row (and get a custom row layout as well).</p> 553 <p> 554 If you get compiler errors about classes not being found, ctrl-shift-O or 555 (cmd-shift-O on the mac) to organize imports. 556 </p> 557 558 <h2 style="clear:right;">Step 13</h2> 559 <p>Run it! 560 <ol> 561 <li> 562 Right click on the <code>Notepadv1</code> project.</li> 563 <li> 564 From the popup menu, select <strong>Run As</strong> > 565 <strong>Android Application</strong>.</li> 566 <li> 567 If you see a dialog come up, select Android Launcher as the way of running 568 the application (you can also use the link near the top of the dialog to 569 set this as your default for the workspace; this is recommended as it will 570 stop the plugin from asking you this every time).</li> 571 <li>Add new notes by hitting the menu button and selecting <em>Add 572 Item</em> from the menu.</li> 573 </ol> 574 575 <h2 style="clear:right;">Solution and Next Steps</h2> 576 <p>You can see the solution to this class in <code>Notepadv1Solution</code> 577 from 578 the zip file to compare with your own.</p> 579 580 <p>Once you are ready, move on to <a href="notepad-ex2.html">Tutorial 581 Exercise 2</a> to add the ability to create, edit and delete notes.</p> 582 583