· 7 years ago · Jan 13, 2019, 03:44 PM
1Viewing Inserted text data in Other XML arraylist
2public class MainActivity extends Activity {
3
4 private EditText mNameText;
5 private EditText mBodyText;
6 private Long mRowId;
7
8public void onCreate(Bundle savedInstanceState) {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.main);
11 mNameText = (EditText) findViewById(R.id.editText1);
12 mBodyText = (EditText) findViewById(R.id.editText2);
13
14 Button SaveButton = (Button) findViewById(R.id.button1);
15 Button ViewButton = (Button) findViewById(R.id.button2);
16
17 mRowId = null;
18 Bundle extras = getIntent().getExtras();
19 if (extras != null) {
20 String title = extras.getString(NotesDbAdapter.KEY_NAME);
21 String body = extras.getString(NotesDbAdapter.KEY_BODY);
22 mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
23 if (title != null) {
24 mNameText.setText(title);
25 }
26 if (body != null) {
27 mBodyText.setText(body);
28 }
29}
30 SaveButton.setOnClickListener(new View.OnClickListener() {
31
32 public void onClick(View view) {
33 Bundle bundle = new Bundle();
34
35 bundle.putString(NotesDbAdapter.KEY_NAME, mNameText.getText().toString());
36 bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText().toString());
37 if (mRowId != null) {
38 bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
39 }
40
41 Intent mIntent = new Intent();
42 mIntent.putExtras(bundle);
43 setResult(RESULT_OK, mIntent);
44 finish();
45 }
46
47 });
48
49 ViewButton.setOnClickListener(new View.OnClickListener() {
50 public void onClick(View view) {
51 Intent i = new Intent(MainActivity.this,NotesV2.class);
52 startActivity(i);
53 }
54});
55
56 }
57
58 }
59
60public class NotesV2 extends ListActivity {
61 private static final int ACTIVITY_CREATE=0;
62private static final int ACTIVITY_EDIT=1;
63
64private static final int INSERT_ID = Menu.FIRST;
65private static final int DELETE_ID = Menu.FIRST + 1;
66
67private NotesDbAdapter mDbHelper;
68private Cursor mNotesCursor;
69
70public void onCreate(Bundle savedInstanceState) {
71 super.onCreate(savedInstanceState);
72 setContentView(R.layout.notes_list);
73 mDbHelper = new NotesDbAdapter(this);
74 mDbHelper.open();
75 fillData();
76 registerForContextMenu(getListView());
77}
78
79 private void fillData() {
80 // Get all of the rows from the database and create the item list
81 mNotesCursor = mDbHelper.fetchAllNotes();
82 startManagingCursor(mNotesCursor);
83
84 // Create an array to specify the fields we want to display in the list (only TITLE)
85 String[] from = new String[]{NotesDbAdapter.KEY_NAME};
86
87
88 // and an array of the fields we want to bind those fields to (in this case just text1)
89
90 int[] to = new int[]{R.id.list1};
91
92 SimpleCursorAdapter notes =
93 new SimpleCursorAdapter(this, R.layout.notes_row, mNotesCursor, from, to);
94 setListAdapter(notes);
95 }
96
97public boolean onCreateOptionsMenu(Menu menu) {
98 super.onCreateOptionsMenu(menu);
99 menu.add(0, INSERT_ID, 0, R.string.menu_insert);
100 return true;
101}
102
103public boolean onMenuItemSelected(int featureId, MenuItem item) {
104 switch(item.getItemId()) {
105 case INSERT_ID:
106 createNote();
107 return true;
108 }
109
110 return super.onMenuItemSelected(featureId, item);
111}
112
113@Override
114public void onCreateContextMenu(ContextMenu menu, View v,
115 ContextMenuInfo menuInfo) {
116 super.onCreateContextMenu(menu, v, menuInfo);
117 menu.add(0, DELETE_ID, 0, R.string.menu_delete);
118}
119
120@Override
121public boolean onContextItemSelected(MenuItem item) {
122 switch(item.getItemId()) {
123 case DELETE_ID:
124 AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
125 mDbHelper.deleteNote(info.id);
126 fillData();
127 return true;
128 }
129 return super.onContextItemSelected(item);
130}
131
132private void createNote() {
133 Intent i = new Intent(this, MainActivity.class);
134 startActivityForResult(i, ACTIVITY_CREATE);
135}
136
137@Override
138protected void onListItemClick(ListView l, View v, int position, long id) {
139 super.onListItemClick(l, v, position, id);
140 Cursor c = mNotesCursor;
141 c.moveToPosition(position);
142 Intent i = new Intent(this, MainActivity.class);
143 i.putExtra(NotesDbAdapter.KEY_ROWID, id);
144 i.putExtra(NotesDbAdapter.KEY_NAME, c.getString(
145 c.getColumnIndexOrThrow(NotesDbAdapter.KEY_NAME)));
146 i.putExtra(NotesDbAdapter.KEY_BODY, c.getString(
147 c.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)));
148 startActivityForResult(i, ACTIVITY_EDIT);
149 }
150
151@Override
152protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
153 super.onActivityResult(requestCode, resultCode, intent);
154 Bundle extras = intent.getExtras();
155 switch(requestCode) {
156 case ACTIVITY_CREATE:
157 String title = extras.getString(NotesDbAdapter.KEY_NAME);
158 String body = extras.getString(NotesDbAdapter.KEY_BODY);
159 mDbHelper.createNote(title, body);
160 fillData();
161 break;
162 case ACTIVITY_EDIT:
163 Long rowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
164 if (rowId != null) {
165 String editTitle = extras.getString(NotesDbAdapter.KEY_NAME);
166 String editBody = extras.getString(NotesDbAdapter.KEY_BODY);
167 mDbHelper.updateNote(rowId, editTitle, editBody);
168 }
169 fillData();
170 break;
171 }
172 }
173 }
174
175public class NotesDbAdapter {
176
177public static final String KEY_NAME = "name";
178public static final String KEY_BODY = "position";
179public static final String KEY_ROWID = "_id";
180
181private static final String TAG = "NotesDbAdapter";
182private DatabaseHelper mDbHelper;
183private SQLiteDatabase mDb;
184
185/**
186 * Database creation sql statement
187 */
188private static final String DATABASE_CREATE =
189 "create table notes (_id integer primary key autoincrement, "
190 + "NAME text not null, position text not null);";
191
192private static final String DATABASE_NAME = "data";
193private static final String DATABASE_TABLE = "employee";
194private static final int DATABASE_VERSION = 2;
195
196private final Context mCtx;
197
198private static class DatabaseHelper extends SQLiteOpenHelper {
199
200 DatabaseHelper(Context context) {
201 super(context, DATABASE_NAME, null, DATABASE_VERSION);
202 }
203
204 @Override
205 public void onCreate(SQLiteDatabase db) {
206
207 db.execSQL(DATABASE_CREATE);
208 }
209
210 @Override
211 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
212 Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
213 + newVersion + ", which will destroy all old data");
214 db.execSQL("DROP TABLE IF EXISTS notes");
215 onCreate(db);
216 }
217 }
218
219 public NotesDbAdapter(Context ctx) {
220 this.mCtx = ctx;
221 }
222
223
224 public NotesDbAdapter open() throws SQLException {
225 mDbHelper = new DatabaseHelper(mCtx);
226 mDb = mDbHelper.getWritableDatabase();
227 return this;
228 }
229
230 public void close() {
231 mDbHelper.close();
232 }
233
234
235
236 public long createNote(String name, String body) {
237 ContentValues initialValues = new ContentValues();
238 initialValues.put(KEY_NAME, name);
239 initialValues.put(KEY_BODY, body);
240
241 return mDb.insert(DATABASE_TABLE, null, initialValues);
242 }
243
244
245public boolean deleteNote(long rowId) {
246
247 return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
248}
249
250
251 public Cursor fetchAllNotes() {
252
253 return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
254 KEY_BODY}, null, null, null, null, null);
255 }
256
257 public Cursor fetchNote(long rowId) throws SQLException {
258
259 Cursor mCursor =
260
261 mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
262 KEY_NAME, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
263 null, null, null, null);
264 if (mCursor != null) {
265 mCursor.moveToFirst();
266 }
267 return mCursor;
268
269 }
270
271
272 public boolean updateNote(long rowId, String title, String body) {
273 ContentValues args = new ContentValues();
274 args.put(KEY_NAME, title);
275 args.put(KEY_BODY, body);
276
277 return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
278}
279 }