diff options
Diffstat (limited to 'tutorials/NotepadCodeLab/Notepadv3Solution/src')
3 files changed, 57 insertions, 59 deletions
diff --git a/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NoteEdit.java b/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NoteEdit.java index f5eb6c433..eabb07a4e 100755 --- a/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NoteEdit.java +++ b/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NoteEdit.java @@ -25,7 +25,7 @@ import android.widget.EditText; public class NoteEdit extends Activity { - private EditText mTitleText; + private EditText mTitleText; private EditText mBodyText; private Long mRowId; private NotesDbAdapter mDbHelper; @@ -36,63 +36,62 @@ public class NoteEdit extends Activity { mDbHelper = new NotesDbAdapter(this); mDbHelper.open(); setContentView(R.layout.note_edit); - - + + mTitleText = (EditText) findViewById(R.id.title); mBodyText = (EditText) findViewById(R.id.body); - + Button confirmButton = (Button) findViewById(R.id.confirm); - mRowId = (savedInstanceState == null) ? null : (Long) savedInstanceState.getSerializable(NotesDbAdapter.KEY_ROWID); if (mRowId == null) { - Bundle extras = getIntent().getExtras(); - mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) + Bundle extras = getIntent().getExtras(); + mRowId = extras != null ? extras.getLong(NotesDbAdapter.KEY_ROWID) : null; } populateFields(); - + confirmButton.setOnClickListener(new View.OnClickListener() { - public void onClick(View view) { - setResult(RESULT_OK); - finish(); - } - + public void onClick(View view) { + setResult(RESULT_OK); + finish(); + } + }); } - + private void populateFields() { if (mRowId != null) { Cursor note = mDbHelper.fetchNote(mRowId); startManagingCursor(note); mTitleText.setText(note.getString( - note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); + note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); mBodyText.setText(note.getString( note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY))); } } - + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); saveState(); outState.putSerializable(NotesDbAdapter.KEY_ROWID, mRowId); } - + @Override protected void onPause() { super.onPause(); saveState(); } - + @Override protected void onResume() { super.onResume(); populateFields(); } - + private void saveState() { String title = mTitleText.getText().toString(); String body = mBodyText.getText().toString(); @@ -106,5 +105,5 @@ public class NoteEdit extends Activity { mDbHelper.updateNote(mRowId, title, body); } } - + } diff --git a/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/Notepadv3.java b/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/Notepadv3.java index 7f9903750..1e15694a0 100755 --- a/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/Notepadv3.java +++ b/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/Notepadv3.java @@ -1,7 +1,7 @@ /* * Copyright (C) 2008 Google Inc. * - * Licensed under the Apache License, Version 2.0 (the "License")savedInstanceState; + * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * @@ -32,12 +32,12 @@ import android.widget.AdapterView.AdapterContextMenuInfo; public class Notepadv3 extends ListActivity { private static final int ACTIVITY_CREATE=0; private static final int ACTIVITY_EDIT=1; - + private static final int INSERT_ID = Menu.FIRST; private static final int DELETE_ID = Menu.FIRST + 1; private NotesDbAdapter mDbHelper; - + /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { @@ -48,23 +48,23 @@ public class Notepadv3 extends ListActivity { fillData(); registerForContextMenu(getListView()); } - + private void fillData() { Cursor notesCursor = mDbHelper.fetchAllNotes(); startManagingCursor(notesCursor); - + // Create an array to specify the fields we want to display in the list (only TITLE) String[] from = new String[]{NotesDbAdapter.KEY_TITLE}; - + // and an array of the fields we want to bind those fields to (in this case just text1) int[] to = new int[]{R.id.text1}; - + // Now create a simple cursor adapter and set it to display SimpleCursorAdapter notes = - new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to); + new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to); setListAdapter(notes); } - + @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); @@ -75,38 +75,38 @@ public class Notepadv3 extends ListActivity { @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { switch(item.getItemId()) { - case INSERT_ID: - createNote(); - return true; + case INSERT_ID: + createNote(); + return true; } - + return super.onMenuItemSelected(featureId, item); } - + @Override - public void onCreateContextMenu(ContextMenu menu, View v, - ContextMenuInfo menuInfo) { - super.onCreateContextMenu(menu, v, menuInfo); + public void onCreateContextMenu(ContextMenu menu, View v, + ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); menu.add(0, DELETE_ID, 0, R.string.menu_delete); - } + } @Override - public boolean onContextItemSelected(MenuItem item) { - switch(item.getItemId()) { - case DELETE_ID: - AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); - mDbHelper.deleteNote(info.id); - fillData(); - return true; - } - return super.onContextItemSelected(item); - } - + public boolean onContextItemSelected(MenuItem item) { + switch(item.getItemId()) { + case DELETE_ID: + AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); + mDbHelper.deleteNote(info.id); + fillData(); + return true; + } + return super.onContextItemSelected(item); + } + private void createNote() { Intent i = new Intent(this, NoteEdit.class); startActivityForResult(i, ACTIVITY_CREATE); } - + @Override protected void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); @@ -116,8 +116,7 @@ public class Notepadv3 extends ListActivity { } @Override - protected void onActivityResult(int requestCode, int resultCode, - Intent intent) { + protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); fillData(); } diff --git a/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NotesDbAdapter.java b/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NotesDbAdapter.java index 61ad04623..38ee1c02f 100755 --- a/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NotesDbAdapter.java +++ b/tutorials/NotepadCodeLab/Notepadv3Solution/src/com/android/demo/notepad3/NotesDbAdapter.java @@ -43,13 +43,13 @@ public class NotesDbAdapter { private static final String TAG = "NotesDbAdapter"; private DatabaseHelper mDbHelper; private SQLiteDatabase mDb; - + /** * Database creation sql statement */ private static final String DATABASE_CREATE = - "create table notes (_id integer primary key autoincrement, " - + "title text not null, body text not null);"; + "create table notes (_id integer primary key autoincrement, " + + "title text not null, body text not null);"; private static final String DATABASE_NAME = "data"; private static final String DATABASE_TABLE = "notes"; @@ -102,7 +102,7 @@ public class NotesDbAdapter { mDb = mDbHelper.getWritableDatabase(); return this; } - + public void close() { mDbHelper.close(); } @@ -158,9 +158,9 @@ public class NotesDbAdapter { Cursor mCursor = - mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, - KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null, - null, null, null, null); + mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, + KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null, + null, null, null, null); if (mCursor != null) { mCursor.moveToFirst(); } |
