• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.notepad2;
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 Notepadv2 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     private Cursor mNotesCursor;
41 
42     /** Called when the activity is first created. */
43     @Override
onCreate(Bundle savedInstanceState)44     public void onCreate(Bundle savedInstanceState) {
45         super.onCreate(savedInstanceState);
46         setContentView(R.layout.notes_list);
47         mDbHelper = new NotesDbAdapter(this);
48         mDbHelper.open();
49         fillData();
50         registerForContextMenu(getListView());
51     }
52 
fillData()53     private void fillData() {
54         // Get all of the rows from the database and create the item list
55         mNotesCursor = mDbHelper.fetchAllNotes();
56         startManagingCursor(mNotesCursor);
57 
58         // Create an array to specify the fields we want to display in the list (only TITLE)
59         String[] from = new String[]{NotesDbAdapter.KEY_TITLE};
60 
61         // and an array of the fields we want to bind those fields to (in this case just text1)
62         int[] to = new int[]{R.id.text1};
63 
64         // Now create a simple cursor adapter and set it to display
65         SimpleCursorAdapter notes =
66             new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
67         setListAdapter(notes);
68     }
69 
70     @Override
onCreateOptionsMenu(Menu menu)71     public boolean onCreateOptionsMenu(Menu menu) {
72         super.onCreateOptionsMenu(menu);
73         menu.add(0, INSERT_ID, 0, R.string.menu_insert);
74         return true;
75     }
76 
77     @Override
onMenuItemSelected(int featureId, MenuItem item)78     public boolean onMenuItemSelected(int featureId, MenuItem item) {
79         switch(item.getItemId()) {
80             case INSERT_ID:
81                 createNote();
82                 return true;
83         }
84 
85         return super.onMenuItemSelected(featureId, item);
86     }
87 
88     @Override
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)89     public void onCreateContextMenu(ContextMenu menu, View v,
90             ContextMenuInfo menuInfo) {
91         super.onCreateContextMenu(menu, v, menuInfo);
92         menu.add(0, DELETE_ID, 0, R.string.menu_delete);
93     }
94 
95     @Override
onContextItemSelected(MenuItem item)96     public boolean onContextItemSelected(MenuItem item) {
97         switch(item.getItemId()) {
98             case DELETE_ID:
99                 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
100                 mDbHelper.deleteNote(info.id);
101                 fillData();
102                 return true;
103         }
104         return super.onContextItemSelected(item);
105     }
106 
createNote()107     private void createNote() {
108         Intent i = new Intent(this, NoteEdit.class);
109         startActivityForResult(i, ACTIVITY_CREATE);
110     }
111 
112     @Override
onListItemClick(ListView l, View v, int position, long id)113     protected void onListItemClick(ListView l, View v, int position, long id) {
114         super.onListItemClick(l, v, position, id);
115         Cursor c = mNotesCursor;
116         c.moveToPosition(position);
117         Intent i = new Intent(this, NoteEdit.class);
118         i.putExtra(NotesDbAdapter.KEY_ROWID, id);
119         i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString(
120                 c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
121         i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
122                 c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
123         startActivityForResult(i, ACTIVITY_EDIT);
124     }
125 
126     @Override
onActivityResult(int requestCode, int resultCode, Intent intent)127     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
128         super.onActivityResult(requestCode, resultCode, intent);
129         Bundle extras = intent.getExtras();
130         switch(requestCode) {
131             case ACTIVITY_CREATE:
132                 String title = extras.getString(NotesDbAdapter.KEY_TITLE);
133                 String body = extras.getString(NotesDbAdapter.KEY_BODY);
134                 mDbHelper.createNote(title, body);
135                 fillData();
136                 break;
137             case ACTIVITY_EDIT:
138                 Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
139                 if (rowId != null) {
140                     String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE);
141                     String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
142                     mDbHelper.updateNote(rowId, editTitle, editBody);
143                 }
144                 fillData();
145                 break;
146         }
147     }
148 }
149