1 /* 2 * Copyright (C) 2008 Google Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.demo.notepad3; 18 19 import android.app.ListActivity; 20 import android.content.Intent; 21 import android.database.Cursor; 22 import android.os.Bundle; 23 import android.view.ContextMenu; 24 import android.view.Menu; 25 import android.view.MenuItem; 26 import android.view.View; 27 import android.view.ContextMenu.ContextMenuInfo; 28 import android.widget.ListView; 29 import android.widget.SimpleCursorAdapter; 30 import android.widget.AdapterView.AdapterContextMenuInfo; 31 32 public class Notepadv3 extends ListActivity { 33 private static final int ACTIVITY_CREATE=0; 34 private static final int ACTIVITY_EDIT=1; 35 36 private static final int INSERT_ID = Menu.FIRST; 37 private static final int DELETE_ID = Menu.FIRST + 1; 38 39 private NotesDbAdapter mDbHelper; 40 41 /** Called when the activity is first created. */ 42 @Override 43 public void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.notes_list); 46 mDbHelper = new NotesDbAdapter(this); 47 mDbHelper.open(); 48 fillData(); 49 registerForContextMenu(getListView()); 50 } 51 52 private void fillData() { 53 Cursor notesCursor = mDbHelper.fetchAllNotes(); 54 startManagingCursor(notesCursor); 55 56 // Create an array to specify the fields we want to display in the list (only TITLE) 57 String[] from = new String[]{NotesDbAdapter.KEY_TITLE}; 58 59 // and an array of the fields we want to bind those fields to (in this case just text1) 60 int[] to = new int[]{R.id.text1}; 61 62 // Now create a simple cursor adapter and set it to display 63 SimpleCursorAdapter notes = 64 new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to); 65 setListAdapter(notes); 66 } 67 68 @Override 69 public boolean onCreateOptionsMenu(Menu menu) { 70 super.onCreateOptionsMenu(menu); 71 menu.add(0, INSERT_ID, 0, R.string.menu_insert); 72 return true; 73 } 74 75 @Override 76 public boolean onMenuItemSelected(int featureId, MenuItem item) { 77 switch(item.getItemId()) { 78 case INSERT_ID: 79 createNote(); 80 return true; 81 } 82 83 return super.onMenuItemSelected(featureId, item); 84 } 85 86 @Override 87 public void onCreateContextMenu(ContextMenu menu, View v, 88 ContextMenuInfo menuInfo) { 89 super.onCreateContextMenu(menu, v, menuInfo); 90 menu.add(0, DELETE_ID, 0, R.string.menu_delete); 91 } 92 93 @Override 94 public boolean onContextItemSelected(MenuItem item) { 95 switch(item.getItemId()) { 96 case DELETE_ID: 97 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); 98 mDbHelper.deleteNote(info.id); 99 fillData(); 100 return true; 101 } 102 return super.onContextItemSelected(item); 103 } 104 105 private void createNote() { 106 Intent i = new Intent(this, NoteEdit.class); 107 startActivityForResult(i, ACTIVITY_CREATE); 108 } 109 110 @Override 111 protected void onListItemClick(ListView l, View v, int position, long id) { 112 super.onListItemClick(l, v, position, id); 113 Intent i = new Intent(this, NoteEdit.class); 114 i.putExtra(NotesDbAdapter.KEY_ROWID, id); 115 startActivityForResult(i, ACTIVITY_EDIT); 116 } 117 118 @Override 119 protected void onActivityResult(int requestCode, int resultCode, Intent intent) { 120 super.onActivityResult(requestCode, resultCode, intent); 121 fillData(); 122 } 123 } 124