Home | History | Annotate | Download | only in notepad

Lines Matching full:code

10 <li><em>The basics of <code>ListActivities</code> and creating and handling menu
32 <p>Open up the <code>Notepadv1</code> project in Eclipse.</p>
34 <p><code>Notepadv1</code> is a project that is provided as a starting point. It
46 Click <strong>Browse</strong> and navigate to where you copied the <code>NotepadCodeLab</code>
48 and select <code>Notepadv1</code>.</li>
55 Click <strong>Finish</strong>. The <code>Notepadv1</code> project should open and be
59 <p>If you see an error about <code>AndroidManifest.xml</code>, or some
79 The NotePad sample in the <code>samples/</code> folder of the SDK also has an example of how
84 <p>Take a look at the <code>NotesDbAdapter</code> class &mdash; this class is provided to
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>
97 <p>The constructor for <code>NotesDbAdapter</code> takes a Context, which allows it to communicate with aspects
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>,
104 <p><code>close()</code> just closes the database, releasing resources related to the
106 <p><code>createNote()</code> takes strings for the title and body of a new note,
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
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
126 <p><code>fetchNote()</code> is similar to <code>fetchAllNotes()</code> but just gets one note
128 {@link android.database.sqlite.SQLiteDatabase} <code>query()</code> method.
133 <p>And finally, <code>updateNote()</code> takes a <var>rowId</var>, <var>title</var> and <var>body</var>, and uses a
156 <p>Open the <code>notepad_list.xml</code> file in <code>res/layout</code>
168 <code>&lt;?xml version="1.0" encoding="utf-8"?&gt;</code>. </li>
171 definition of some kind, in this case a <code>LinearLayout</code>. </li>
174 the top level component or layout in the XML so that <code>android:</code> tags can
176 <p><code>xmlns:android="http://schemas.android.com/apk/res/android"</code></p>
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>
201 The <strong>&#64;</strong> symbol in the id strings of the <code>ListView</code> and
202 <code>TextView</code> tags means
206 The <code>ListView</code> and <code>TextView</code> can be
210 resource in <code>res/values/strings.xml</code>) will be displayed if there
212 <li>The <code>list</code> and <code>empty</code> IDs are
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
221 More broadly, the <code>android.R</code> class is a set of predefined
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
251 Create a new file under <code>res/layout</code> called
252 <code>notes_row.xml</code>. </li>
265 <p>In this case we create a new id called <code>text1</code>. The
268 <code>text1</code> on the fly and then using it.</p>
272 <p>Open the <code>R.java</code> class in the
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>
278 <p>Next, open the <code>Notepadv1</code> class in the source. In the following steps, we are going to
282 <p><code>Notepadv1</code> will inherit from a subclass
283 of <code>Activity</code> called a <code>ListActivity</code>,
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
293 <code>onCreate</code>, <code>onCreateOptionsMenu</code> and
294 <code>onOptionsItemSelected</code>; we need to fill these
297 <li><code>onCreate()</code> is called when the activity is
301 <li><code>onCreateOptionsMenu()</code> is used to populate the
306 <li><code>onOptionsItemSelected()</code> is the other half of the
313 <p>Change the inheritance of <code>Notepadv1</code
314 <code>Activity</code>
315 to <code>ListActivity</code>:</p>
317 <p>Note: you will have to import <code>ListActivity</code> into the
324 <p>Fill out the body of the <code>onCreate()</code> method.</p>
326 screen), use the <code>notepad_list</code> layout we created in XML,
327 set up the <code>NotesDbAdapter</code> instance that will
332 In the <code>onCreate</code> method, call <code>super.onCreate()</code> with the
333 <code>savedInstanceState</code> parameter that's passed in.</li>
335 Call <code>setContentView()</code> and pass <code>R.layout.notepad_list</code>.</li>
337 At the top of the class, create a new private class field called <code>mDbHelper</code> of class
338 <code>NotesDbAdapter</code>.
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>)
347 Call the <code>open()</code> method on <code>mDbHelper</code> to open (or create) the
351 Finally, call a new method <code>fillData()</code>, which will get the data and
355 <code>onCreate()</code> should now look like this:</p>
365 <p>And be sure you have the <code>mDbHelper</code> field definition (right
384 <p>Fill out the body of the <code>onCreateOptionsMenu()</code> method.</p>
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>:
394 <p>Then save the file and return to <code>Notepadv1</code>.</p>
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>
411 <p>The arguments passed to <code>add()</code> indicate: a group identifier for this menu (none,
416 <p>Fill out the body of the <code>onOptionsItemSelected()</code> method:</p>
419 <code>onOptionsItemSelected()</code> method will be called with the
420 <code>item.getId()</code> set to <code>INSERT_ID</code> (the constant we
425 The <code>super.onOptionsItemSelected(item)</code> method call goes at the
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>,
433 <li>Return the result of the superclass' <code>onOptionsItemSelected()</code>
437 The whole <code>onOptionsItemSelect()</code> method should now look like
452 <p>Add a new <code>createNote()</code> method:</p>
454 our application, <code>createNote()</code> is not going to be very useful.
461 <li>Construct the name using "Note" and the counter we defined in the class: <code>
462 String noteName = "Note " + mNoteNumber++</code></li>
464 Call <code>mDbHelper.createNote()</code> using <code>noteName</code> as the
465 title and <code>""</code> for the body
468 Call <code>fillData()</code> to populate the list of notes (inefficient but
472 The whole <code>createNote()</code> method should look like this: </p>
494 <p>Define the <code>fillData()</code> method:</p>
496 method uses <code>SimpleCursorAdapter,</code> which takes a database <code>Cursor</code>
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
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
504 <code>NotesDbAdapter.KEY_TITLE</code>) and, the second, an int array
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>
527 After obtaining the Cursor from <code>mDbHelper.fetchAllNotes()</code>, we
529 <code>startManagingCursor()</code> that allows Android to take care of the
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
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
562 Right click on the <code>Notepadv1</code> project.</li>
576 <p>You can see the solution to this class in <code>Notepadv1Solution</code>