質問編集履歴

7

エラーの追記

2019/01/16 08:20

投稿

amabile046
amabile046

スコア18

test CHANGED
File without changes
test CHANGED
@@ -10,13 +10,11 @@
10
10
 
11
11
 
12
12
 
13
- 追記:デバッグしたところ、MainAvtivity.javaのloadNote()内において、c.getString(c.getColumnIndex(DBAdapter.LASTUPDATE = "lastupdate)),としている行でアプリが強制終了されていることがわかりました。
13
+ 追記:デバッグしたところ、MainAvtivity.javaのloadNote()内において、c.getString(c.getColumnIndex(DBAdapter.LASTUPDATE = "lastupdate)),としている行でアプリが強制終了されていることがわかりました。エラーは以下のものが出ています。
14
-
15
-
16
-
17
- 追記2:Note.javaで以下のエラーが出ます
14
+
18
-
15
+
16
+
19
- ![イメージ説明](393258126021d0d29a8bb081239fe09f.png)
17
+ ![イメージ説明](da9ed0ca59a6a08f6e415d3c6e9899cc.png)
20
18
 
21
19
 
22
20
 

6

追記

2019/01/16 08:20

投稿

amabile046
amabile046

スコア18

test CHANGED
File without changes
test CHANGED
@@ -14,6 +14,12 @@
14
14
 
15
15
 
16
16
 
17
+ 追記2:Note.javaで以下のエラーが出ます
18
+
19
+ ![イメージ説明](393258126021d0d29a8bb081239fe09f.png)
20
+
21
+
22
+
17
23
  #コード
18
24
 
19
25
  ```
@@ -22,14 +28,6 @@
22
28
 
23
29
 
24
30
 
25
- import java.util.ArrayList;
26
-
27
- import java.util.List;
28
-
29
-
30
-
31
-
32
-
33
31
  //文字数の関係上、importの部分は消して投稿させていただきます。参考にしたサイトとまったく同じように書いております。
34
32
 
35
33
 

5

追記内容の修正

2019/01/16 08:00

投稿

amabile046
amabile046

スコア18

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
 
12
12
 
13
- 追記:デバッグしたところ、MainAvtivity.javaのloadNote()内において、c.getString(c.getColumnIndex(DBAdapter.COL_NOTE2)),としている行でアプリが強制終了されていることがわかりました。
13
+ 追記:デバッグしたところ、MainAvtivity.javaのloadNote()内において、c.getString(c.getColumnIndex(DBAdapter.LASTUPDATE = "lastupdate)),としている行でアプリが強制終了されていることがわかりました。
14
14
 
15
15
 
16
16
 

4

タイトルの変更

2019/01/16 07:50

投稿

amabile046
amabile046

スコア18

test CHANGED
@@ -1 +1 @@
1
- SQLiteを用いたアプリが開けない
1
+ c.getStringでアプリが強制終了される
test CHANGED
File without changes

3

追記

2019/01/16 07:42

投稿

amabile046
amabile046

スコア18

test CHANGED
File without changes
test CHANGED
@@ -8,7 +8,9 @@
8
8
 
9
9
  そこで、自分なりにプログラムを書き換えたのですが、コンパイルして実機(ZenFone)で実行すると「アプリケーションが終了しました」とエラーが出てしまいます。
10
10
 
11
+
12
+
11
- SQLiteにいて、またプログラミングについの知識が浅ため起きたエラーであとは思うの、自分で見直しも分からなかったため、どこを間違えている分かる方がいましたら教えていただけると幸いです
13
+ 追記:デバッグしたところ、MainAvtivity.javaのloadNote()内いて、c.getString(c.getColumnIndex(DBAdapter.COL_NOTE2)),としているアプリ強制終了されていることがわました。
12
14
 
13
15
 
14
16
 

2

プログラムの追加

2019/01/16 06:55

投稿

amabile046
amabile046

スコア18

test CHANGED
File without changes
test CHANGED
@@ -26,329 +26,451 @@
26
26
 
27
27
 
28
28
 
29
+
30
+
31
+ //文字数の関係上、importの部分は消して投稿させていただきます。参考にしたサイトとまったく同じように書いております。
32
+
33
+
34
+
35
+ public class MainActivity extends Activity implements OnClickListener { static final String TAG = "SQLiteTest1";
36
+
37
+ static final int MENUITEM_ID_DELETE = 1;
38
+
39
+ ListView itemListView;
40
+
41
+ EditText EnglishEdit;
42
+
43
+ EditText JapaneseEdit;
44
+
45
+ Button saveButton;
46
+
47
+ static DBAdapter dbAdapter;
48
+
49
+ static NoteListAdapter listAdapter;
50
+
51
+ static List<Note> noteList = new ArrayList<Note>();
52
+
53
+
54
+
55
+ @Override
56
+
57
+ public void onCreate(Bundle savedInstanceState) {
58
+
59
+ super.onCreate(savedInstanceState);
60
+
61
+ setContentView(R.layout.activity_main);
62
+
63
+ findViews();
64
+
65
+ setListeners();
66
+
67
+
68
+
69
+ dbAdapter = new DBAdapter(this);
70
+
71
+ listAdapter = new NoteListAdapter();
72
+
73
+ itemListView.setAdapter(listAdapter);
74
+
75
+
76
+
77
+ loadNote();
78
+
79
+ }
80
+
81
+
82
+
83
+ protected void findViews(){
84
+
85
+ itemListView = (ListView)findViewById(R.id.itemListView);
86
+
87
+ EnglishEdit = (EditText)findViewById(R.id.EnglishEdit);
88
+
89
+ JapaneseEdit = (EditText)findViewById(R.id.JapaneseEdit);
90
+
91
+ saveButton = (Button)findViewById(R.id.saveButton);
92
+
93
+ }
94
+
95
+
96
+
97
+ protected void loadNote(){
98
+
99
+ noteList.clear();
100
+
101
+
102
+
103
+ // Read
104
+
105
+ dbAdapter.open();
106
+
107
+ Cursor c = dbAdapter.getAllNotes();
108
+
109
+
110
+
111
+ startManagingCursor(c);
112
+
113
+
114
+
115
+ if(c.moveToFirst()){
116
+
117
+ do {
118
+
119
+ Note note = new Note(
120
+
121
+ c.getInt(c.getColumnIndex(DBAdapter.COL_ID)),
122
+
123
+ c.getString(c.getColumnIndex(DBAdapter.COL_NOTE)),
124
+
125
+ c.getString(c.getColumnIndex(DBAdapter.COL_NOTE2)),
126
+
127
+ c.getString(c.getColumnIndex(DBAdapter.COL_LASTUPDATE))
128
+
129
+ );
130
+
131
+ noteList.add(note);
132
+
133
+ } while(c.moveToNext());
134
+
135
+ }
136
+
137
+
138
+
139
+ stopManagingCursor(c);
140
+
141
+ dbAdapter.close();
142
+
143
+
144
+
145
+ listAdapter.notifyDataSetChanged();
146
+
147
+ }
148
+
149
+
150
+
151
+ protected void saveItem(){
152
+
153
+ dbAdapter.open();
154
+
155
+ dbAdapter.saveNote(EnglishEdit.getText().toString(),JapaneseEdit.getText().toString());
156
+
157
+ dbAdapter.close();
158
+
159
+ EnglishEdit.setText("");
160
+
161
+ JapaneseEdit.setText("");
162
+
163
+ loadNote();
164
+
165
+ }
166
+
167
+
168
+
169
+ protected void setListeners(){
170
+
171
+ saveButton.setOnClickListener(this);
172
+
173
+
174
+
175
+ itemListView.setOnCreateContextMenuListener(
176
+
177
+ new OnCreateContextMenuListener(){
178
+
179
+ @Override
180
+
181
+ public void onCreateContextMenu(
182
+
183
+ ContextMenu menu,
184
+
185
+ View v,
186
+
187
+ ContextMenuInfo menuInfo) {
188
+
189
+ menu.add(0, MENUITEM_ID_DELETE, 0, "削除");
190
+
191
+ }
192
+
193
+ });
194
+
195
+ }
196
+
197
+
198
+
199
+
200
+
201
+ @Override
202
+
203
+ public boolean onContextItemSelected(MenuItem item) {
204
+
205
+
206
+
207
+ switch(item.getItemId()){
208
+
209
+ case MENUITEM_ID_DELETE:
210
+
211
+ AdapterView.AdapterContextMenuInfo menuInfo
212
+
213
+ = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
214
+
215
+
216
+
217
+ Note note = noteList.get(menuInfo.position);
218
+
219
+ final int noteId = note.getId();
220
+
221
+
222
+
223
+ new AlertDialog.Builder(this)
224
+
225
+ .setTitle("この単語を削除しますか?")
226
+
227
+ .setPositiveButton(
228
+
229
+ "はい",
230
+
231
+ new DialogInterface.OnClickListener() {
232
+
233
+ @Override
234
+
235
+ public void onClick(DialogInterface dialog, int which) {
236
+
237
+ dbAdapter.open();
238
+
239
+ if(dbAdapter.deleteNote(noteId)){
240
+
241
+ Toast.makeText(
242
+
243
+ getBaseContext(),
244
+
245
+ "単語は正常に削除されました.",
246
+
247
+ Toast.LENGTH_SHORT);
248
+
249
+ loadNote();
250
+
251
+ }
252
+
253
+ dbAdapter.close();
254
+
255
+ }
256
+
257
+ })
258
+
259
+ .setNegativeButton(
260
+
261
+ "キャンセル",
262
+
263
+ null)
264
+
265
+ .show();
266
+
267
+
268
+
269
+ return true;
270
+
271
+ }
272
+
273
+ return super.onContextItemSelected(item);
274
+
275
+ }
276
+
277
+
278
+
279
+
280
+
281
+ @Override
282
+
283
+ public void onClick(View v) {
284
+
285
+ switch(v.getId()){
286
+
287
+ case R.id.saveButton:
288
+
289
+ saveItem();
290
+
291
+ break;
292
+
293
+ }
294
+
295
+ }
296
+
297
+
298
+
299
+
300
+
301
+ private class NoteListAdapter extends BaseAdapter {
302
+
303
+
304
+
305
+ @Override
306
+
307
+ public int getCount() {
308
+
309
+ return noteList.size();
310
+
311
+ }
312
+
313
+
314
+
315
+ @Override
316
+
317
+ public Object getItem(int position) {
318
+
319
+ return noteList.get(position);
320
+
321
+ }
322
+
323
+
324
+
325
+ @Override
326
+
327
+ public long getItemId(int position) {
328
+
329
+ return position;
330
+
331
+ }
332
+
333
+
334
+
335
+ @Override
336
+
337
+ public View getView(int position, View convertView, ViewGroup parent) {
338
+
339
+ TextView noteTextView;
340
+
341
+ TextView lastupdateTextView;
342
+
343
+ View v = convertView;
344
+
345
+ if(v==null){
346
+
347
+ LayoutInflater inflater =
348
+
349
+ (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
350
+
351
+ v = inflater.inflate(R.layout.row, null);
352
+
353
+ }
354
+
355
+ Note note = (Note)getItem(position);
356
+
357
+ if(note != null){
358
+
359
+ noteTextView = (TextView)v.findViewById(R.id.noteTextView);
360
+
361
+ lastupdateTextView = (TextView)v.findViewById(
362
+
363
+ R.id.lastupdateTextView);
364
+
365
+ noteTextView.setText(note.getNote());
366
+
367
+ noteTextView.setText(note.getNote2());
368
+
369
+ lastupdateTextView.setText(note.getLastupdate());
370
+
371
+ }
372
+
373
+ return v;
374
+
375
+ }
376
+
377
+
378
+
379
+ }
380
+
381
+ }
382
+
383
+ ```
384
+
385
+ ```
386
+
387
+ package com.example.username.memoword;
388
+
389
+
390
+
29
- import android.app.Activity;
391
+ import java.util.Date;
30
-
392
+
393
+
394
+
31
- import android.app.AlertDialog;
395
+ import android.content.ContentValues;
32
396
 
33
397
  import android.content.Context;
34
398
 
35
- import android.content.DialogInterface;
36
-
37
399
  import android.database.Cursor;
38
400
 
39
- import android.os.Bundle;
40
-
41
- import android.view.ContextMenu;
42
-
43
- import android.view.LayoutInflater;
44
-
45
- import android.view.MenuItem;
46
-
47
- import android.view.View;
48
-
49
- import android.view.ViewGroup;
50
-
51
- import android.view.ContextMenu.ContextMenuInfo;
52
-
53
- import android.view.View.OnClickListener;
54
-
55
- import android.view.View.OnCreateContextMenuListener;
56
-
57
- import android.view.View.OnLongClickListener;
58
-
59
- import android.widget.AdapterView;
60
-
61
- import android.widget.BaseAdapter;
62
-
63
- import android.widget.Button;
64
-
65
- import android.widget.EditText;
66
-
67
- import android.widget.ListView;
68
-
69
- import android.widget.TextView;
70
-
71
- import android.widget.Toast;
72
-
73
-
74
-
75
- public class MainActivity extends Activity implements OnClickListener { static final String TAG = "SQLiteTest1";
76
-
77
- static final int MENUITEM_ID_DELETE = 1;
78
-
79
- ListView itemListView;
80
-
81
- EditText EnglishEdit;
82
-
83
- EditText JapaneseEdit;
84
-
85
- Button saveButton;
86
-
87
- static DBAdapter dbAdapter;
88
-
89
- static NoteListAdapter listAdapter;
90
-
91
- static List<Note> noteList = new ArrayList<Note>();
92
-
93
-
94
-
95
- @Override
96
-
97
- public void onCreate(Bundle savedInstanceState) {
98
-
99
- super.onCreate(savedInstanceState);
100
-
101
- setContentView(R.layout.activity_main);
102
-
103
- findViews();
104
-
105
- setListeners();
106
-
107
-
108
-
109
- dbAdapter = new DBAdapter(this);
110
-
111
- listAdapter = new NoteListAdapter();
112
-
113
- itemListView.setAdapter(listAdapter);
114
-
115
-
116
-
117
- loadNote();
118
-
119
- }
120
-
121
-
122
-
123
- protected void findViews(){
124
-
125
- itemListView = (ListView)findViewById(R.id.itemListView);
126
-
127
- EnglishEdit = (EditText)findViewById(R.id.EnglishEdit);
128
-
129
- JapaneseEdit = (EditText)findViewById(R.id.JapaneseEdit);
130
-
131
- saveButton = (Button)findViewById(R.id.saveButton);
132
-
133
- }
134
-
135
-
136
-
137
- protected void loadNote(){
138
-
139
- noteList.clear();
140
-
141
-
142
-
143
- // Read
144
-
145
- dbAdapter.open();
146
-
147
- Cursor c = dbAdapter.getAllNotes();
148
-
149
-
150
-
151
- startManagingCursor(c);
152
-
153
-
154
-
155
- if(c.moveToFirst()){
156
-
157
- do {
158
-
159
- Note note = new Note(
160
-
161
- c.getInt(c.getColumnIndex(DBAdapter.COL_ID)),
162
-
163
- c.getString(c.getColumnIndex(DBAdapter.COL_NOTE)),
164
-
165
- c.getString(c.getColumnIndex(DBAdapter.COL_NOTE2)),
166
-
167
- c.getString(c.getColumnIndex(DBAdapter.COL_LASTUPDATE))
168
-
169
- );
170
-
171
- noteList.add(note);
172
-
173
- } while(c.moveToNext());
174
-
175
- }
176
-
177
-
178
-
179
- stopManagingCursor(c);
180
-
181
- dbAdapter.close();
182
-
183
-
184
-
185
- listAdapter.notifyDataSetChanged();
186
-
187
- }
188
-
189
-
190
-
191
- protected void saveItem(){
192
-
193
- dbAdapter.open();
194
-
195
- dbAdapter.saveNote(EnglishEdit.getText().toString(),JapaneseEdit.getText().toString());
196
-
197
- dbAdapter.close();
198
-
199
- EnglishEdit.setText("");
200
-
201
- JapaneseEdit.setText("");
202
-
203
- loadNote();
204
-
205
- }
206
-
207
-
208
-
209
-
210
-
211
- protected void setListeners(){
212
-
213
- saveButton.setOnClickListener(this);
214
-
215
-
216
-
217
- itemListView.setOnCreateContextMenuListener(
218
-
219
- new OnCreateContextMenuListener(){
220
-
221
- @Override
222
-
223
- public void onCreateContextMenu(
224
-
225
- ContextMenu menu,
226
-
227
- View v,
228
-
229
- ContextMenuInfo menuInfo) {
230
-
231
- menu.add(0, MENUITEM_ID_DELETE, 0, "削除");
232
-
233
- }
234
-
235
- });
236
-
237
- }
238
-
239
-
240
-
241
-
242
-
243
- @Override
244
-
245
- public boolean onContextItemSelected(MenuItem item) {
246
-
247
-
248
-
249
- switch(item.getItemId()){
250
-
251
- case MENUITEM_ID_DELETE:
252
-
253
- AdapterView.AdapterContextMenuInfo menuInfo
254
-
255
- = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
256
-
257
-
258
-
259
- Note note = noteList.get(menuInfo.position);
260
-
261
- final int noteId = note.getId();
262
-
263
-
264
-
265
- new AlertDialog.Builder(this)
266
-
267
- .setTitle("この単語を削除しますか?")
268
-
269
- .setPositiveButton(
270
-
271
- "はい",
272
-
273
- new DialogInterface.OnClickListener() {
274
-
275
- @Override
276
-
277
- public void onClick(DialogInterface dialog, int which) {
278
-
279
- dbAdapter.open();
280
-
281
- if(dbAdapter.deleteNote(noteId)){
282
-
283
- Toast.makeText(
284
-
285
- getBaseContext(),
286
-
287
- "単語は正常に削除されました.",
288
-
289
- Toast.LENGTH_SHORT);
290
-
291
- loadNote();
292
-
293
- }
294
-
295
- dbAdapter.close();
296
-
297
- }
298
-
299
- })
300
-
301
- .setNegativeButton(
302
-
303
- "キャンセル",
304
-
305
- null)
306
-
307
- .show();
308
-
309
-
310
-
311
- return true;
312
-
313
- }
314
-
315
- return super.onContextItemSelected(item);
316
-
317
- }
318
-
319
-
320
-
321
-
322
-
323
- @Override
324
-
325
- public void onClick(View v) {
326
-
327
- switch(v.getId()){
328
-
329
- case R.id.saveButton:
330
-
331
- saveItem();
332
-
333
- break;
334
-
335
- }
336
-
337
- }
338
-
339
-
340
-
341
-
342
-
343
- private class NoteListAdapter extends BaseAdapter {
401
+ import android.database.sqlite.SQLiteDatabase;
402
+
403
+ import android.database.sqlite.SQLiteOpenHelper;
404
+
405
+
406
+
407
+ public class DBAdapter {
408
+
409
+
410
+
411
+ static final String DATABASE_NAME = "mynote.db";
412
+
413
+ static final int DATABASE_VERSION = 1;
414
+
415
+
416
+
417
+ public static final String TABLE_NAME = "notes";
418
+
419
+ public static final String COL_ID = "_id";
420
+
421
+ public static final String COL_NOTE = "note";
422
+
423
+ public static final String COL_NOTE2 = "note2";
424
+
425
+ public static final String COL_LASTUPDATE = "lastupdate";
426
+
427
+
428
+
429
+ protected final Context context;
430
+
431
+ protected DatabaseHelper dbHelper;
432
+
433
+ protected SQLiteDatabase db;
434
+
435
+
436
+
437
+ public DBAdapter(Context context){
438
+
439
+ this.context = context;
440
+
441
+ dbHelper = new DatabaseHelper(this.context);
442
+
443
+ }
444
+
445
+
446
+
447
+ private static class DatabaseHelper extends SQLiteOpenHelper {
448
+
449
+
450
+
451
+ public DatabaseHelper(Context context) {
452
+
453
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
454
+
455
+ }
344
456
 
345
457
 
346
458
 
347
459
  @Override
348
460
 
349
- public int getCount() {
461
+ public void onCreate(SQLiteDatabase db) {
462
+
350
-
463
+ db.execSQL(
464
+
465
+ "CREATE TABLE " + TABLE_NAME + " ("
466
+
467
+ + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
468
+
351
- return noteList.size();
469
+ + COL_NOTE + " TEXT NOT NULL,"
470
+
471
+ + COL_NOTE2 + " TEXT NOT NULL,"
472
+
473
+ + COL_LASTUPDATE + " TEXT NOT NULL);");
352
474
 
353
475
  }
354
476
 
@@ -356,67 +478,85 @@
356
478
 
357
479
  @Override
358
480
 
359
- public Object getItem(int position) {
360
-
361
- return noteList.get(position);
362
-
363
- }
364
-
365
-
366
-
367
- @Override
368
-
369
- public long getItemId(int position) {
370
-
371
- return position;
372
-
373
- }
374
-
375
-
376
-
377
- @Override
378
-
379
- public View getView(int position, View convertView, ViewGroup parent) {
380
-
381
- TextView noteTextView;
382
-
383
- TextView lastupdateTextView;
384
-
385
- View v = convertView;
386
-
387
- if(v==null){
388
-
389
- LayoutInflater inflater =
390
-
391
- (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
392
-
393
- v = inflater.inflate(R.layout.row, null);
394
-
395
- }
396
-
397
- Note note = (Note)getItem(position);
398
-
399
- if(note != null){
400
-
401
- noteTextView = (TextView)v.findViewById(R.id.noteTextView);
402
-
403
- lastupdateTextView = (TextView)v.findViewById(
404
-
405
- R.id.lastupdateTextView);
406
-
407
- noteTextView.setText(note.getNote());
408
-
409
- noteTextView.setText(note.getNote2());
410
-
411
- lastupdateTextView.setText(note.getLastupdate());
412
-
413
- }
414
-
415
- return v;
416
-
417
- }
418
-
419
-
481
+ public void onUpgrade(
482
+
483
+ SQLiteDatabase db,
484
+
485
+ int oldVersion,
486
+
487
+ int newVersion) {
488
+
489
+ db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
490
+
491
+ onCreate(db);
492
+
493
+ }
494
+
495
+
496
+
497
+ }
498
+
499
+
500
+
501
+
502
+
503
+ public DBAdapter open() {
504
+
505
+ db = dbHelper.getWritableDatabase();
506
+
507
+ return this;
508
+
509
+ }
510
+
511
+
512
+
513
+
514
+
515
+ public void close(){
516
+
517
+ dbHelper.close();
518
+
519
+ }
520
+
521
+
522
+
523
+ public boolean deleteAllNotes(){
524
+
525
+ return db.delete(TABLE_NAME, null, null) > 0;
526
+
527
+ }
528
+
529
+
530
+
531
+ public boolean deleteNote(int id){
532
+
533
+ return db.delete(TABLE_NAME, COL_ID + "=" + id, null) > 0;
534
+
535
+ }
536
+
537
+
538
+
539
+ public Cursor getAllNotes(){
540
+
541
+ return db.query(TABLE_NAME, null, null, null, null, null, null);
542
+
543
+ }
544
+
545
+
546
+
547
+ public void saveNote(String note,String note2){
548
+
549
+ Date dateNow = new Date ();
550
+
551
+ ContentValues values = new ContentValues();
552
+
553
+ values.put(COL_NOTE, note);
554
+
555
+ values.put(COL_NOTE2, note2);
556
+
557
+ values.put(COL_LASTUPDATE, dateNow.toLocaleString());
558
+
559
+ db.insertOrThrow(TABLE_NAME, null, values);
420
560
 
421
561
  }
422
562
 
@@ -426,188 +566,70 @@
426
566
 
427
567
 
428
568
 
429
-
430
-
431
-
432
-
433
569
  ```
434
570
 
435
571
  package com.example.username.memoword;
436
572
 
437
573
 
438
574
 
439
- import java.util.Date;
440
-
441
-
442
-
443
- import android.content.ContentValues;
444
-
445
- import android.content.Context;
446
-
447
- import android.database.Cursor;
448
-
449
- import android.database.sqlite.SQLiteDatabase;
450
-
451
- import android.database.sqlite.SQLiteOpenHelper;
452
-
453
-
454
-
455
- public class DBAdapter {
456
-
457
-
458
-
459
- static final String DATABASE_NAME = "mynote.db";
460
-
461
- static final int DATABASE_VERSION = 1;
462
-
463
-
464
-
465
- public static final String TABLE_NAME = "notes";
466
-
467
- public static final String COL_ID = "_id";
468
-
469
- public static final String COL_NOTE = "note";
470
-
471
- public static final String COL_NOTE2 = "note2";
472
-
473
- public static final String COL_LASTUPDATE = "lastupdate";
474
-
475
-
476
-
477
- protected final Context context;
478
-
479
- protected DatabaseHelper dbHelper;
480
-
481
- protected SQLiteDatabase db;
482
-
483
-
484
-
485
- public DBAdapter(Context context){
486
-
487
- this.context = context;
488
-
489
- dbHelper = new DatabaseHelper(this.context);
490
-
491
- }
492
-
493
-
494
-
495
- private static class DatabaseHelper extends SQLiteOpenHelper {
496
-
497
-
498
-
499
- public DatabaseHelper(Context context) {
500
-
501
- super(context, DATABASE_NAME, null, DATABASE_VERSION);
502
-
503
- }
504
-
505
-
506
-
507
- @Override
508
-
509
- public void onCreate(SQLiteDatabase db) {
510
-
511
- db.execSQL(
512
-
513
- "CREATE TABLE " + TABLE_NAME + " ("
514
-
515
- + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
516
-
517
- + COL_NOTE + " TEXT NOT NULL,"
518
-
519
- + COL_NOTE2 + " TEXT NOT NULL,"
520
-
521
- + COL_LASTUPDATE + " TEXT NOT NULL);");
522
-
523
- }
524
-
525
-
526
-
527
- @Override
528
-
529
- public void onUpgrade(
530
-
531
- SQLiteDatabase db,
532
-
533
- int oldVersion,
534
-
535
- int newVersion) {
536
-
537
- db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
538
-
539
- onCreate(db);
540
-
541
- }
542
-
543
-
544
-
545
- }
546
-
547
-
548
-
549
-
550
-
551
- public DBAdapter open() {
552
-
553
- db = dbHelper.getWritableDatabase();
554
-
555
- return this;
556
-
557
- }
558
-
559
-
560
-
561
-
562
-
563
- public void close(){
564
-
565
- dbHelper.close();
566
-
567
- }
568
-
569
-
570
-
571
- public boolean deleteAllNotes(){
572
-
573
- return db.delete(TABLE_NAME, null, null) > 0;
574
-
575
- }
576
-
577
-
578
-
579
- public boolean deleteNote(int id){
580
-
581
- return db.delete(TABLE_NAME, COL_ID + "=" + id, null) > 0;
582
-
583
- }
584
-
585
-
586
-
587
- public Cursor getAllNotes(){
588
-
589
- return db.query(TABLE_NAME, null, null, null, null, null, null);
590
-
591
- }
592
-
593
-
594
-
595
- public void saveNote(String note,String note2){
596
-
597
- Date dateNow = new Date ();
598
-
599
- ContentValues values = new ContentValues();
600
-
601
- values.put(COL_NOTE, note);
602
-
603
- values.put(COL_NOTE2, note2);
604
-
605
- values.put(COL_LASTUPDATE, dateNow.toLocaleString());
606
-
607
- db.insertOrThrow(TABLE_NAME, null, values);
575
+ public class Note {
576
+
577
+ protected int id;
578
+
579
+ protected String note;
580
+
581
+ protected String note2;
582
+
583
+ protected String lastupdate;
584
+
585
+
586
+
587
+ public Note(int id, String note, String note2, String lastupdate){
588
+
589
+ this.id = id;
590
+
591
+ this.note = note;
592
+
593
+ this.note2 = note2;
594
+
595
+ this.lastupdate = lastupdate;
596
+
597
+ }
598
+
599
+
600
+
601
+ public String getNote(){
602
+
603
+ return note;
604
+
605
+ }
606
+
607
+
608
+
609
+ public String getNote2(){
610
+
611
+ return note2;
612
+
613
+ }
614
+
615
+
616
+
617
+ public String getLastupdate(){
618
+
619
+ return lastupdate;
620
+
621
+ }
622
+
623
+
624
+
625
+ public int getId(){
626
+
627
+ return id;
608
628
 
609
629
  }
610
630
 
611
631
  }
612
632
 
633
+
634
+
613
635
  ```

1

プログラムの修正

2019/01/16 01:26

投稿

amabile046
amabile046

スコア18

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
  #コード
16
16
 
17
- ```MainActivity.java
17
+ ```
18
18
 
19
19
  package com.example.username.memoword;
20
20
 
@@ -428,70 +428,186 @@
428
428
 
429
429
 
430
430
 
431
+
432
+
431
- ```Note.java
433
+ ```
432
434
 
433
435
  package com.example.username.memoword;
434
436
 
435
437
 
436
438
 
437
- public class Note {
438
-
439
- protected int id;
440
-
441
- protected String note;
442
-
443
- protected String note2;
444
-
445
- protected String lastupdate;
446
-
447
-
448
-
449
- public Note(int id, String note, String note2, String lastupdate){
450
-
451
- this.id = id;
452
-
453
- this.note = note;
454
-
455
- this.note2 = note2;
456
-
457
- this.lastupdate = lastupdate;
458
-
459
- }
460
-
461
-
462
-
463
- public String getNote(){
464
-
465
- return note;
466
-
467
- }
468
-
469
-
470
-
471
- public String getNote2(){
472
-
473
- return note2;
474
-
475
- }
476
-
477
-
478
-
479
- public String getLastupdate(){
480
-
481
- return lastupdate;
482
-
483
- }
484
-
485
-
486
-
487
- public int getId(){
488
-
489
- return id;
439
+ import java.util.Date;
440
+
441
+
442
+
443
+ import android.content.ContentValues;
444
+
445
+ import android.content.Context;
446
+
447
+ import android.database.Cursor;
448
+
449
+ import android.database.sqlite.SQLiteDatabase;
450
+
451
+ import android.database.sqlite.SQLiteOpenHelper;
452
+
453
+
454
+
455
+ public class DBAdapter {
456
+
457
+
458
+
459
+ static final String DATABASE_NAME = "mynote.db";
460
+
461
+ static final int DATABASE_VERSION = 1;
462
+
463
+
464
+
465
+ public static final String TABLE_NAME = "notes";
466
+
467
+ public static final String COL_ID = "_id";
468
+
469
+ public static final String COL_NOTE = "note";
470
+
471
+ public static final String COL_NOTE2 = "note2";
472
+
473
+ public static final String COL_LASTUPDATE = "lastupdate";
474
+
475
+
476
+
477
+ protected final Context context;
478
+
479
+ protected DatabaseHelper dbHelper;
480
+
481
+ protected SQLiteDatabase db;
482
+
483
+
484
+
485
+ public DBAdapter(Context context){
486
+
487
+ this.context = context;
488
+
489
+ dbHelper = new DatabaseHelper(this.context);
490
+
491
+ }
492
+
493
+
494
+
495
+ private static class DatabaseHelper extends SQLiteOpenHelper {
496
+
497
+
498
+
499
+ public DatabaseHelper(Context context) {
500
+
501
+ super(context, DATABASE_NAME, null, DATABASE_VERSION);
502
+
503
+ }
504
+
505
+
506
+
507
+ @Override
508
+
509
+ public void onCreate(SQLiteDatabase db) {
510
+
511
+ db.execSQL(
512
+
513
+ "CREATE TABLE " + TABLE_NAME + " ("
514
+
515
+ + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
516
+
517
+ + COL_NOTE + " TEXT NOT NULL,"
518
+
519
+ + COL_NOTE2 + " TEXT NOT NULL,"
520
+
521
+ + COL_LASTUPDATE + " TEXT NOT NULL);");
522
+
523
+ }
524
+
525
+
526
+
527
+ @Override
528
+
529
+ public void onUpgrade(
530
+
531
+ SQLiteDatabase db,
532
+
533
+ int oldVersion,
534
+
535
+ int newVersion) {
536
+
537
+ db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
538
+
539
+ onCreate(db);
540
+
541
+ }
542
+
543
+
544
+
545
+ }
546
+
547
+
548
+
549
+
550
+
551
+ public DBAdapter open() {
552
+
553
+ db = dbHelper.getWritableDatabase();
554
+
555
+ return this;
556
+
557
+ }
558
+
559
+
560
+
561
+
562
+
563
+ public void close(){
564
+
565
+ dbHelper.close();
566
+
567
+ }
568
+
569
+
570
+
571
+ public boolean deleteAllNotes(){
572
+
573
+ return db.delete(TABLE_NAME, null, null) > 0;
574
+
575
+ }
576
+
577
+
578
+
579
+ public boolean deleteNote(int id){
580
+
581
+ return db.delete(TABLE_NAME, COL_ID + "=" + id, null) > 0;
582
+
583
+ }
584
+
585
+
586
+
587
+ public Cursor getAllNotes(){
588
+
589
+ return db.query(TABLE_NAME, null, null, null, null, null, null);
590
+
591
+ }
592
+
593
+
594
+
595
+ public void saveNote(String note,String note2){
596
+
597
+ Date dateNow = new Date ();
598
+
599
+ ContentValues values = new ContentValues();
600
+
601
+ values.put(COL_NOTE, note);
602
+
603
+ values.put(COL_NOTE2, note2);
604
+
605
+ values.put(COL_LASTUPDATE, dateNow.toLocaleString());
606
+
607
+ db.insertOrThrow(TABLE_NAME, null, values);
490
608
 
491
609
  }
492
610
 
493
611
  }
494
612
 
495
-
496
-
497
613
  ```