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 |
---|
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 |
|
---|
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 |
---|
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 |
---|
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 |
---|
89 |
public void onCreateContextMenu(ContextMenu menu, View v, |
---|
90 |
ContextMenuInfo menuInfo) { |
---|
91 |
super.onCreateContextMenu(menu, v, menuInfo); |
---|
92 |
|
---|
93 |
// TODO: fill in rest of method |
---|
94 |
menu.add(0,DELETE_ID,0,R.string.menu_delete); |
---|
95 |
} |
---|
96 |
|
---|
97 |
@Override |
---|
98 |
public boolean onContextItemSelected(MenuItem item) { |
---|
99 |
switch(item.getItemId()){ |
---|
100 |
case DELETE_ID: |
---|
101 |
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); |
---|
102 |
mDbHelper.deleteNote(info.id); |
---|
103 |
fillData(); |
---|
104 |
return true; |
---|
105 |
} |
---|
106 |
return super.onContextItemSelected(item); |
---|
107 |
|
---|
108 |
// TODO: fill in rest of method |
---|
109 |
} |
---|
110 |
|
---|
111 |
private void createNote() { |
---|
112 |
// TODO: fill in implementation |
---|
113 |
Intent i = new Intent(this,NoteEdit.class); |
---|
114 |
startActivityForResult(i, ACTIVITY_CREATE); |
---|
115 |
} |
---|
116 |
|
---|
117 |
@Override |
---|
118 |
protected void onListItemClick(ListView l, View v, int position, long id) { |
---|
119 |
super.onListItemClick(l, v, position, id); |
---|
120 |
|
---|
121 |
// TODO: fill in rest of method |
---|
122 |
Cursor c = mNotesCursor; |
---|
123 |
c.moveToPosition(position); |
---|
124 |
Intent i = new Intent(this,NoteEdit.class); |
---|
125 |
i.putExtra(NotesDbAdapter.KEY_ROWID, id); |
---|
126 |
i.putExtra(NotesDbAdapter.KEY_TITLE, c.getString( |
---|
127 |
c.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE) |
---|
128 |
) |
---|
129 |
); |
---|
130 |
i.putExtra(NotesDbAdapter.KEY_BODY, c.getString( |
---|
131 |
c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY) |
---|
132 |
) |
---|
133 |
); |
---|
134 |
startActivityForResult(i, ACTIVITY_EDIT); |
---|
135 |
} |
---|
136 |
|
---|
137 |
@Override |
---|
138 |
protected void onActivityResult(int requestCode, int resultCode, Intent intent) { |
---|
139 |
super.onActivityResult(requestCode, resultCode, intent); |
---|
140 |
|
---|
141 |
// TODO: fill in rest of method |
---|
142 |
Bundle extras = intent.getExtras(); |
---|
143 |
|
---|
144 |
switch(requestCode){ |
---|
145 |
case ACTIVITY_CREATE: |
---|
146 |
String title =extras.getString(NotesDbAdapter.KEY_TITLE); |
---|
147 |
String body = extras.getString(NotesDbAdapter.KEY_BODY); |
---|
148 |
mDbHelper.createNote(title, body); |
---|
149 |
fillData(); |
---|
150 |
break; |
---|
151 |
case ACTIVITY_EDIT: |
---|
152 |
Long mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID); |
---|
153 |
if(mRowId != null){ |
---|
154 |
String editTitle = extras.getString(NotesDbAdapter.KEY_TITLE); |
---|
155 |
String editBody = extras.getString(NotesDbAdapter.KEY_BODY); |
---|
156 |
mDbHelper.updateNote(mRowId, editTitle, editBody); |
---|
157 |
} |
---|
158 |
fillData(); |
---|
159 |
break; |
---|
160 |
} |
---|
161 |
} |
---|
162 |
} |
---|