質問編集履歴

3

回答がつかないため一度

2021/10/31 08:46

投稿

kaame
kaame

スコア0

test CHANGED
File without changes
test CHANGED
@@ -1,44 +1,10 @@
1
1
  ### 前提・実現したいこと
2
-
3
- 開発環境 androidstudio
4
-
5
- 言語 java
6
2
 
7
3
 
8
4
 
9
- todoリストを作っています。
10
-
11
- 遷移して引き継いだ、時間とやるべきことをRist_Activityの追加ボタン押すことによって、Rist_Activityに表示しています。
12
-
13
- やるべきこと、時間はデータベースに保存ができたのですが、チェックボックスの状態の保存方法が分からずに困っています。
14
5
 
15
6
 
16
7
 
17
- また、状態判定については、
18
-
19
- CheckBox checkbox = findViewById(R.id.checkBox);
20
-
21
-
22
-
23
- if(checkbox.isSelected())
24
-
25
- Log.i("MyTAG", "trueです。");
26
-
27
- else
28
-
29
- Log.i("MyTAG", "はfalseです。");
30
-
31
- のように判定できると思うのですが、
32
-
33
-
34
-
35
- 今回は追加ボタンを押すごとにレコードが増えるのに対し、idはcheckBoxの一つなので、各レコードのチェックボックスの区別がつかずにいます。
36
-
37
- また、get()メソッドもうまく使えないため、どのように区別すればいいのでしょうか。
38
-
39
-
40
-
41
- このコードたちを例に状態判定方法、データベースの登録方法の二つを教えてくれるとありがたいです。
42
8
 
43
9
 
44
10
 
@@ -60,492 +26,6 @@
60
26
 
61
27
  ```ここに言語名を入力
62
28
 
63
- RistActivity.java
64
-
65
-
66
-
67
- public class RistActivity extends AppCompatActivity {
68
-
69
- private static final Object Tag = "MainActivity";
70
-
71
- private TaskDbHelper mHelper;
72
-
73
- private ListView mTaskListView;
74
-
75
- private ArrayAdapter<String> mAdapter;
76
-
77
-
78
-
79
-
80
-
81
- @Override
82
-
83
- protected void onCreate(Bundle savedInstanceState) {
84
-
85
- super.onCreate(savedInstanceState);
86
-
87
- setContentView(R.layout.activity_rist);
88
-
89
- Intent intentrist = getIntent();
90
-
91
- String tenm = intentrist.getStringExtra("tenm");//渡されたデータ取得 tenm
92
-
93
- String inputStr = intentrist.getStringExtra("inputStr");//渡
94
-
95
-
96
-
97
- String bt_check01 =intentrist.getStringExtra("bt_check");
98
-
99
- Button button = findViewById(R.id.button);
100
-
101
- TextView textView5 = findViewById(R.id.textView5);
102
-
103
- findViewById(R.id.textView5).setVisibility(View.INVISIBLE); //見えなくする
104
-
105
- findViewById(R.id.textView6).setVisibility(View.INVISIBLE);
106
-
107
- textView5.setText(bt_check01);
108
-
109
- String getok = String.valueOf(textView5.getText()); //条件分岐用変数
110
-
111
-
112
-
113
-
114
-
115
- if (getok.equals("ok")) {
116
-
117
- button.setEnabled(true);
118
-
119
-
120
-
121
- }else {
122
-
123
- button.setEnabled(false);
124
-
125
- }
126
-
127
-
128
-
129
- //戻るメニュー表示
130
-
131
- ActionBar actionBar = getSupportActionBar();
132
-
133
- actionBar.setDisplayHomeAsUpEnabled(true);
134
-
135
- //終了
136
-
137
-
138
-
139
- //日付 出力コード 開始
140
-
141
- LocalDateTime time = LocalDateTime.now(); //現在の日付取得
142
-
143
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); // フォーマット決定
144
-
145
- String calender = (time.format(dtf));
146
-
147
-
148
-
149
-
150
-
151
- mHelper = new TaskDbHelper(this);
152
-
153
- mTaskListView =(ListView) findViewById(R.id.listView);
154
-
155
-
156
-
157
- SQLiteDatabase db = mHelper.getReadableDatabase();
158
-
159
- Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
160
-
161
- new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
162
-
163
- null, null, null, null, null);
164
-
165
-
166
-
167
-
168
-
169
- cursor.close();
170
-
171
- db.close();
172
-
173
- TextView task = findViewById(R.id.textView4);
174
-
175
- if (getok.equals("ok")) {
176
-
177
- task.setText(tenm + inputStr + "を追加しますか?");
178
-
179
-
180
-
181
- }else {
182
-
183
- task.setText("追加はタスク入力画面で行ってください");
184
-
185
- }
186
-
187
- updateUI();
188
-
189
-
190
-
191
-
192
-
193
- findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
194
-
195
- @Override
196
-
197
- public void onClick(View v) {
198
-
199
- Context context = getApplicationContext();
200
-
201
- Toast.makeText(context , "保存されました。", Toast.LENGTH_LONG).show();
202
-
203
-
204
-
205
- TextView textView6 = findViewById(R.id.textView6);
206
-
207
- findViewById(R.id.textView6).setVisibility(View.INVISIBLE);
208
-
209
- textView6.setText(tenm+" "+" "+" "+inputStr);
210
-
211
- String gettask = String.valueOf(textView6.getText());
212
-
213
-
214
-
215
-
216
-
217
- SQLiteDatabase db = mHelper.getWritableDatabase();
218
-
219
- ContentValues values = new ContentValues();
220
-
221
- values.put(TaskContract.TaskEntry.COL_TASK_TITLE, gettask);
222
-
223
- db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
224
-
225
- null,
226
-
227
- values,
228
-
229
- SQLiteDatabase.CONFLICT_REPLACE);
230
-
231
- db.close();
232
-
233
- updateUI();
234
-
235
-
236
-
237
-
238
-
239
- }
240
-
241
- });
242
-
243
- }
244
-
245
-
246
-
247
- //戻るメニューメソッド 開始
248
-
249
- @Override
250
-
251
- public boolean onOptionsItemSelected(MenuItem item){
252
-
253
- int itemId = item.getItemId();
254
-
255
- if(itemId==android.R.id.home){
256
-
257
- finish();
258
-
259
- }
260
-
261
- return super.onOptionsItemSelected(item);
262
-
263
- }//終了
264
-
265
-
266
-
267
- private void updateUI() {
268
-
269
- ArrayList<String> taskList = new ArrayList<>();
270
-
271
-
272
-
273
- for (int i=0;i<taskList.size();i++){
274
-
275
-
276
-
277
- }
278
-
279
- SQLiteDatabase db = mHelper.getReadableDatabase();
280
-
281
- Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
282
-
283
- new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
284
-
285
- null, null, null, null, null);
286
-
287
- while (cursor.moveToNext()) {
288
-
289
- int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
290
-
291
- taskList.add(cursor.getString(idx));
292
-
293
- }
294
-
295
- if (mAdapter == null) {
296
-
297
- mAdapter = new ArrayAdapter<>(this,
298
-
299
- R.layout.list,
300
-
301
- R.id.task_title,
302
-
303
- taskList);
304
-
305
- mTaskListView.setAdapter(mAdapter);
306
-
307
- } else {
308
-
309
- mAdapter.clear();
310
-
311
- mAdapter.addAll(taskList);
312
-
313
- mAdapter.notifyDataSetChanged();
314
-
315
- }
316
-
317
- cursor.close();
318
-
319
- db.close();
320
-
321
- }
322
-
323
- public void deleteTask(View view) {
324
-
325
- View parent = (View) view.getParent();
326
-
327
- TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
328
-
329
- String task = String.valueOf(taskTextView.getText());
330
-
331
- SQLiteDatabase db = mHelper.getWritableDatabase();
332
-
333
- db.delete(TaskContract.TaskEntry.TABLE,
334
-
335
- TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
336
-
337
- new String[]{task});
338
-
339
- db.close();
340
-
341
- updateUI();
342
-
343
- }
344
-
345
-
346
-
347
- }
348
-
349
29
 
350
30
 
351
31
  ```
352
-
353
-
354
-
355
- ```ここに言語名を入力
356
-
357
- TaskContract.java
358
-
359
-
360
-
361
- public class TaskContract {
362
-
363
- public static final String DB_NAME = "com.aziflaj.todolist.db";
364
-
365
- public static final int DB_VERSION = 1;
366
-
367
-
368
-
369
- public class TaskEntry implements BaseColumns {
370
-
371
- public static final String TABLE = "tasks";
372
-
373
- public static final String COL_TASK_TITLE = "title";
374
-
375
- }
376
-
377
- }
378
-
379
- ```
380
-
381
-
382
-
383
-
384
-
385
- ```ここに言語名を入力
386
-
387
- TaskDbHelper.java
388
-
389
-
390
-
391
- public class TaskDbHelper extends SQLiteOpenHelper {
392
-
393
-
394
-
395
- public TaskDbHelper(Context context) {
396
-
397
- super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
398
-
399
- }
400
-
401
-
402
-
403
- @Override
404
-
405
- public void onCreate(SQLiteDatabase db) {
406
-
407
- String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
408
-
409
- TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
410
-
411
- TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);";
412
-
413
-
414
-
415
- db.execSQL(createTable);
416
-
417
- }
418
-
419
-
420
-
421
- @Override
422
-
423
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
424
-
425
- db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
426
-
427
- onCreate(db);
428
-
429
- }
430
-
431
- }
432
-
433
- ```
434
-
435
-
436
-
437
-
438
-
439
- ```ここに言語名を入力
440
-
441
- activity_list.xml
442
-
443
-
444
-
445
- <?xml version="1.0" encoding="utf-8"?>
446
-
447
- <LinearLayout
448
-
449
- xmlns:android="http://schemas.android.com/apk/res/android"
450
-
451
- xmlns:app="http://schemas.android.com/apk/res-auto"
452
-
453
- xmlns:tools="http://schemas.android.com/tools"
454
-
455
- android:layout_width="match_parent"
456
-
457
- android:layout_height="match_parent"
458
-
459
- android:orientation="vertical"
460
-
461
- tools:context=".MainActivity">
462
-
463
-
464
-
465
-
466
-
467
- <ListView
468
-
469
- android:id="@+id/listView"
470
-
471
- android:layout_width="387dp"
472
-
473
- android:layout_height="450dp"
474
-
475
- tools:layout_editor_absoluteX="1dp"
476
-
477
- tools:layout_editor_absoluteY="1dp" />
478
-
479
-
480
-
481
-
482
-
483
- <TextView
484
-
485
- android:id="@+id/textView4"
486
-
487
- android:layout_width="match_parent"
488
-
489
- android:layout_height="wrap_content"
490
-
491
- android:text="TextView" />
492
-
493
-
494
-
495
- <Button
496
-
497
- android:id="@+id/button"
498
-
499
- android:layout_width="265dp"
500
-
501
- android:layout_height="wrap_content"
502
-
503
- android:text="追加" />
504
-
505
-
506
-
507
- <TextView
508
-
509
- android:id="@+id/textView5"
510
-
511
- android:layout_width="match_parent"
512
-
513
- android:layout_height="wrap_content"
514
-
515
- android:text="TextView" />
516
-
517
-
518
-
519
- <TextView
520
-
521
- android:id="@+id/textView6"
522
-
523
- android:layout_width="match_parent"
524
-
525
- android:layout_height="wrap_content"
526
-
527
- android:text="TextView" />
528
-
529
-
530
-
531
- <TextView
532
-
533
- android:id="@+id/textView7"
534
-
535
- android:layout_width="match_parent"
536
-
537
- android:layout_height="wrap_content"
538
-
539
- android:text="TextView" />
540
-
541
-
542
-
543
-
544
-
545
- </LinearLayout>
546
-
547
-
548
-
549
-
550
-
551
- ```

2

レイアウトのコードの追加 activity_list.xmlとlist.xml

2021/10/31 08:46

投稿

kaame
kaame

スコア0

test CHANGED
File without changes
test CHANGED
@@ -436,16 +436,116 @@
436
436
 
437
437
 
438
438
 
439
-
439
+ ```ここに言語名を入力
440
+
440
-
441
+ activity_list.xml
442
+
443
+
444
+
445
+ <?xml version="1.0" encoding="utf-8"?>
446
+
447
+ <LinearLayout
448
+
449
+ xmlns:android="http://schemas.android.com/apk/res/android"
450
+
451
+ xmlns:app="http://schemas.android.com/apk/res-auto"
452
+
453
+ xmlns:tools="http://schemas.android.com/tools"
454
+
455
+ android:layout_width="match_parent"
456
+
457
+ android:layout_height="match_parent"
458
+
459
+ android:orientation="vertical"
460
+
461
+ tools:context=".MainActivity">
462
+
463
+
464
+
465
+
466
+
467
+ <ListView
468
+
469
+ android:id="@+id/listView"
470
+
471
+ android:layout_width="387dp"
472
+
473
+ android:layout_height="450dp"
474
+
475
+ tools:layout_editor_absoluteX="1dp"
476
+
477
+ tools:layout_editor_absoluteY="1dp" />
478
+
479
+
480
+
481
+
482
+
483
+ <TextView
484
+
485
+ android:id="@+id/textView4"
486
+
487
+ android:layout_width="match_parent"
488
+
489
+ android:layout_height="wrap_content"
490
+
491
+ android:text="TextView" />
492
+
493
+
494
+
441
- ### 試したこと
495
+ <Button
442
-
443
-
444
-
445
-
446
-
447
-
448
-
449
-
450
-
496
+
451
- ### 補足情報(FW/ツールのバージョンなど)
497
+ android:id="@+id/button"
498
+
499
+ android:layout_width="265dp"
500
+
501
+ android:layout_height="wrap_content"
502
+
503
+ android:text="追加" />
504
+
505
+
506
+
507
+ <TextView
508
+
509
+ android:id="@+id/textView5"
510
+
511
+ android:layout_width="match_parent"
512
+
513
+ android:layout_height="wrap_content"
514
+
515
+ android:text="TextView" />
516
+
517
+
518
+
519
+ <TextView
520
+
521
+ android:id="@+id/textView6"
522
+
523
+ android:layout_width="match_parent"
524
+
525
+ android:layout_height="wrap_content"
526
+
527
+ android:text="TextView" />
528
+
529
+
530
+
531
+ <TextView
532
+
533
+ android:id="@+id/textView7"
534
+
535
+ android:layout_width="match_parent"
536
+
537
+ android:layout_height="wrap_content"
538
+
539
+ android:text="TextView" />
540
+
541
+
542
+
543
+
544
+
545
+ </LinearLayout>
546
+
547
+
548
+
549
+
550
+
551
+ ```

1

コードを、、、の中にいれました

2021/10/28 09:02

投稿

kaame
kaame

スコア0

test CHANGED
File without changes
test CHANGED
@@ -62,365 +62,377 @@
62
62
 
63
63
  RistActivity.java
64
64
 
65
+
66
+
67
+ public class RistActivity extends AppCompatActivity {
68
+
69
+ private static final Object Tag = "MainActivity";
70
+
71
+ private TaskDbHelper mHelper;
72
+
73
+ private ListView mTaskListView;
74
+
75
+ private ArrayAdapter<String> mAdapter;
76
+
77
+
78
+
79
+
80
+
81
+ @Override
82
+
83
+ protected void onCreate(Bundle savedInstanceState) {
84
+
85
+ super.onCreate(savedInstanceState);
86
+
87
+ setContentView(R.layout.activity_rist);
88
+
89
+ Intent intentrist = getIntent();
90
+
91
+ String tenm = intentrist.getStringExtra("tenm");//渡されたデータ取得 tenm
92
+
93
+ String inputStr = intentrist.getStringExtra("inputStr");//渡
94
+
95
+
96
+
97
+ String bt_check01 =intentrist.getStringExtra("bt_check");
98
+
99
+ Button button = findViewById(R.id.button);
100
+
101
+ TextView textView5 = findViewById(R.id.textView5);
102
+
103
+ findViewById(R.id.textView5).setVisibility(View.INVISIBLE); //見えなくする
104
+
105
+ findViewById(R.id.textView6).setVisibility(View.INVISIBLE);
106
+
107
+ textView5.setText(bt_check01);
108
+
109
+ String getok = String.valueOf(textView5.getText()); //条件分岐用変数
110
+
111
+
112
+
113
+
114
+
115
+ if (getok.equals("ok")) {
116
+
117
+ button.setEnabled(true);
118
+
119
+
120
+
121
+ }else {
122
+
123
+ button.setEnabled(false);
124
+
125
+ }
126
+
127
+
128
+
129
+ //戻るメニュー表示
130
+
131
+ ActionBar actionBar = getSupportActionBar();
132
+
133
+ actionBar.setDisplayHomeAsUpEnabled(true);
134
+
135
+ //終了
136
+
137
+
138
+
139
+ //日付 出力コード 開始
140
+
141
+ LocalDateTime time = LocalDateTime.now(); //現在の日付取得
142
+
143
+ DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); // フォーマット決定
144
+
145
+ String calender = (time.format(dtf));
146
+
147
+
148
+
149
+
150
+
151
+ mHelper = new TaskDbHelper(this);
152
+
153
+ mTaskListView =(ListView) findViewById(R.id.listView);
154
+
155
+
156
+
157
+ SQLiteDatabase db = mHelper.getReadableDatabase();
158
+
159
+ Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
160
+
161
+ new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
162
+
163
+ null, null, null, null, null);
164
+
165
+
166
+
167
+
168
+
169
+ cursor.close();
170
+
171
+ db.close();
172
+
173
+ TextView task = findViewById(R.id.textView4);
174
+
175
+ if (getok.equals("ok")) {
176
+
177
+ task.setText(tenm + inputStr + "を追加しますか?");
178
+
179
+
180
+
181
+ }else {
182
+
183
+ task.setText("追加はタスク入力画面で行ってください");
184
+
185
+ }
186
+
187
+ updateUI();
188
+
189
+
190
+
191
+
192
+
193
+ findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
194
+
195
+ @Override
196
+
197
+ public void onClick(View v) {
198
+
199
+ Context context = getApplicationContext();
200
+
201
+ Toast.makeText(context , "保存されました。", Toast.LENGTH_LONG).show();
202
+
203
+
204
+
205
+ TextView textView6 = findViewById(R.id.textView6);
206
+
207
+ findViewById(R.id.textView6).setVisibility(View.INVISIBLE);
208
+
209
+ textView6.setText(tenm+" "+" "+" "+inputStr);
210
+
211
+ String gettask = String.valueOf(textView6.getText());
212
+
213
+
214
+
215
+
216
+
217
+ SQLiteDatabase db = mHelper.getWritableDatabase();
218
+
219
+ ContentValues values = new ContentValues();
220
+
221
+ values.put(TaskContract.TaskEntry.COL_TASK_TITLE, gettask);
222
+
223
+ db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
224
+
225
+ null,
226
+
227
+ values,
228
+
229
+ SQLiteDatabase.CONFLICT_REPLACE);
230
+
231
+ db.close();
232
+
233
+ updateUI();
234
+
235
+
236
+
237
+
238
+
239
+ }
240
+
241
+ });
242
+
243
+ }
244
+
245
+
246
+
247
+ //戻るメニューメソッド 開始
248
+
249
+ @Override
250
+
251
+ public boolean onOptionsItemSelected(MenuItem item){
252
+
253
+ int itemId = item.getItemId();
254
+
255
+ if(itemId==android.R.id.home){
256
+
257
+ finish();
258
+
259
+ }
260
+
261
+ return super.onOptionsItemSelected(item);
262
+
263
+ }//終了
264
+
265
+
266
+
267
+ private void updateUI() {
268
+
269
+ ArrayList<String> taskList = new ArrayList<>();
270
+
271
+
272
+
273
+ for (int i=0;i<taskList.size();i++){
274
+
275
+
276
+
277
+ }
278
+
279
+ SQLiteDatabase db = mHelper.getReadableDatabase();
280
+
281
+ Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
282
+
283
+ new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
284
+
285
+ null, null, null, null, null);
286
+
287
+ while (cursor.moveToNext()) {
288
+
289
+ int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
290
+
291
+ taskList.add(cursor.getString(idx));
292
+
293
+ }
294
+
295
+ if (mAdapter == null) {
296
+
297
+ mAdapter = new ArrayAdapter<>(this,
298
+
299
+ R.layout.list,
300
+
301
+ R.id.task_title,
302
+
303
+ taskList);
304
+
305
+ mTaskListView.setAdapter(mAdapter);
306
+
307
+ } else {
308
+
309
+ mAdapter.clear();
310
+
311
+ mAdapter.addAll(taskList);
312
+
313
+ mAdapter.notifyDataSetChanged();
314
+
315
+ }
316
+
317
+ cursor.close();
318
+
319
+ db.close();
320
+
321
+ }
322
+
323
+ public void deleteTask(View view) {
324
+
325
+ View parent = (View) view.getParent();
326
+
327
+ TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
328
+
329
+ String task = String.valueOf(taskTextView.getText());
330
+
331
+ SQLiteDatabase db = mHelper.getWritableDatabase();
332
+
333
+ db.delete(TaskContract.TaskEntry.TABLE,
334
+
335
+ TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
336
+
337
+ new String[]{task});
338
+
339
+ db.close();
340
+
341
+ updateUI();
342
+
343
+ }
344
+
345
+
346
+
347
+ }
348
+
349
+
350
+
65
351
  ```
66
352
 
67
- public class RistActivity extends AppCompatActivity {
68
-
69
- private static final Object Tag = "MainActivity";
70
-
71
- private TaskDbHelper mHelper;
72
-
73
- private ListView mTaskListView;
74
-
75
- private ArrayAdapter<String> mAdapter;
76
-
77
-
78
-
79
-
80
-
81
- @Override
82
-
83
- protected void onCreate(Bundle savedInstanceState) {
84
-
85
- super.onCreate(savedInstanceState);
86
-
87
- setContentView(R.layout.activity_rist);
88
-
89
- Intent intentrist = getIntent();
90
-
91
- String tenm = intentrist.getStringExtra("tenm");//渡されたデータ取得 tenm
92
-
93
- String inputStr = intentrist.getStringExtra("inputStr");//渡
94
-
95
-
96
-
97
- String bt_check01 =intentrist.getStringExtra("bt_check");
98
-
99
- Button button = findViewById(R.id.button);
100
-
101
- TextView textView5 = findViewById(R.id.textView5);
102
-
103
- findViewById(R.id.textView5).setVisibility(View.INVISIBLE); //見えなくする
104
-
105
- findViewById(R.id.textView6).setVisibility(View.INVISIBLE);
106
-
107
- textView5.setText(bt_check01);
108
-
109
- String getok = String.valueOf(textView5.getText()); //条件分岐用変数
110
-
111
-
112
-
113
-
114
-
115
- if (getok.equals("ok")) {
116
-
117
- button.setEnabled(true);
118
-
119
-
120
-
121
- }else {
122
-
123
- button.setEnabled(false);
124
-
125
- }
126
-
127
-
128
-
129
- //戻るメニュー表示
130
-
131
- ActionBar actionBar = getSupportActionBar();
132
-
133
- actionBar.setDisplayHomeAsUpEnabled(true);
134
-
135
- //終了
136
-
137
-
138
-
139
- //日付 出力コード 開始
140
-
141
- LocalDateTime time = LocalDateTime.now(); //現在の日付取得
142
-
143
- DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy年MM月dd日"); // フォーマット決定
144
-
145
- String calender = (time.format(dtf));
146
-
147
-
148
-
149
-
150
-
151
- mHelper = new TaskDbHelper(this);
152
-
153
- mTaskListView =(ListView) findViewById(R.id.listView);
154
-
155
-
156
-
157
- SQLiteDatabase db = mHelper.getReadableDatabase();
158
-
159
- Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
160
-
161
- new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
162
-
163
- null, null, null, null, null);
164
-
165
-
166
-
167
-
168
-
169
- cursor.close();
170
-
171
- db.close();
172
-
173
- TextView task = findViewById(R.id.textView4);
174
-
175
- if (getok.equals("ok")) {
176
-
177
- task.setText(tenm + inputStr + "を追加しますか?");
178
-
179
-
180
-
181
- }else {
182
-
183
- task.setText("追加はタスク入力画面で行ってください");
184
-
185
- }
186
-
187
- updateUI();
188
-
189
-
190
-
191
-
192
-
193
- findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
194
-
195
- @Override
196
-
197
- public void onClick(View v) {
198
-
199
- Context context = getApplicationContext();
200
-
201
- Toast.makeText(context , "保存されました。", Toast.LENGTH_LONG).show();
202
-
203
-
204
-
205
- TextView textView6 = findViewById(R.id.textView6);
206
-
207
- findViewById(R.id.textView6).setVisibility(View.INVISIBLE);
208
-
209
- textView6.setText(tenm+" "+" "+" "+inputStr);
210
-
211
- String gettask = String.valueOf(textView6.getText());
212
-
213
-
214
-
215
-
216
-
217
- SQLiteDatabase db = mHelper.getWritableDatabase();
218
-
219
- ContentValues values = new ContentValues();
220
-
221
- values.put(TaskContract.TaskEntry.COL_TASK_TITLE, gettask);
222
-
223
- db.insertWithOnConflict(TaskContract.TaskEntry.TABLE,
224
-
225
- null,
226
-
227
- values,
228
-
229
- SQLiteDatabase.CONFLICT_REPLACE);
230
-
231
- db.close();
232
-
233
- updateUI();
234
-
235
-
236
-
237
-
238
-
239
- }
240
-
241
- });
242
-
243
- }
244
-
245
-
246
-
247
- //戻るメニューメソッド 開始
248
-
249
- @Override
250
-
251
- public boolean onOptionsItemSelected(MenuItem item){
252
-
253
- int itemId = item.getItemId();
254
-
255
- if(itemId==android.R.id.home){
256
-
257
- finish();
258
-
259
- }
260
-
261
- return super.onOptionsItemSelected(item);
262
-
263
- }//終了
264
-
265
-
266
-
267
- private void updateUI() {
268
-
269
- ArrayList<String> taskList = new ArrayList<>();
270
-
271
-
272
-
273
- for (int i=0;i<taskList.size();i++){
274
-
275
-
276
-
277
- }
278
-
279
- SQLiteDatabase db = mHelper.getReadableDatabase();
280
-
281
- Cursor cursor = db.query(TaskContract.TaskEntry.TABLE,
282
-
283
- new String[]{TaskContract.TaskEntry._ID, TaskContract.TaskEntry.COL_TASK_TITLE},
284
-
285
- null, null, null, null, null);
286
-
287
- while (cursor.moveToNext()) {
288
-
289
- int idx = cursor.getColumnIndex(TaskContract.TaskEntry.COL_TASK_TITLE);
290
-
291
- taskList.add(cursor.getString(idx));
292
-
293
- }
294
-
295
- if (mAdapter == null) {
296
-
297
- mAdapter = new ArrayAdapter<>(this,
298
-
299
- R.layout.list,
300
-
301
- R.id.task_title,
302
-
303
- taskList);
304
-
305
- mTaskListView.setAdapter(mAdapter);
306
-
307
- } else {
308
-
309
- mAdapter.clear();
310
-
311
- mAdapter.addAll(taskList);
312
-
313
- mAdapter.notifyDataSetChanged();
314
-
315
- }
316
-
317
- cursor.close();
318
-
319
- db.close();
320
-
321
- }
322
-
323
- public void deleteTask(View view) {
324
-
325
- View parent = (View) view.getParent();
326
-
327
- TextView taskTextView = (TextView) parent.findViewById(R.id.task_title);
328
-
329
- String task = String.valueOf(taskTextView.getText());
330
-
331
- SQLiteDatabase db = mHelper.getWritableDatabase();
332
-
333
- db.delete(TaskContract.TaskEntry.TABLE,
334
-
335
- TaskContract.TaskEntry.COL_TASK_TITLE + " = ?",
336
-
337
- new String[]{task});
338
-
339
- db.close();
340
-
341
- updateUI();
342
-
343
- }
344
-
345
-
353
+
354
+
355
+ ```ここに言語名を入力
356
+
357
+ TaskContract.java
358
+
359
+
360
+
361
+ public class TaskContract {
362
+
363
+ public static final String DB_NAME = "com.aziflaj.todolist.db";
364
+
365
+ public static final int DB_VERSION = 1;
366
+
367
+
368
+
369
+ public class TaskEntry implements BaseColumns {
370
+
371
+ public static final String TABLE = "tasks";
372
+
373
+ public static final String COL_TASK_TITLE = "title";
374
+
375
+ }
346
376
 
347
377
  }
348
378
 
379
+ ```
380
+
381
+
382
+
349
383
 
350
384
 
351
385
  ```ここに言語名を入力
352
386
 
353
- TaskContract.java
387
+ TaskDbHelper.java
388
+
389
+
390
+
391
+ public class TaskDbHelper extends SQLiteOpenHelper {
392
+
393
+
394
+
395
+ public TaskDbHelper(Context context) {
396
+
397
+ super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
398
+
399
+ }
400
+
401
+
402
+
403
+ @Override
404
+
405
+ public void onCreate(SQLiteDatabase db) {
406
+
407
+ String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
408
+
409
+ TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
410
+
411
+ TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);";
412
+
413
+
414
+
415
+ db.execSQL(createTable);
416
+
417
+ }
418
+
419
+
420
+
421
+ @Override
422
+
423
+ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
424
+
425
+ db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
426
+
427
+ onCreate(db);
428
+
429
+ }
430
+
431
+ }
354
432
 
355
433
  ```
356
434
 
357
- public class TaskContract {
435
+
358
-
359
- public static final String DB_NAME = "com.aziflaj.todolist.db";
360
-
361
- public static final int DB_VERSION = 1;
362
-
363
-
364
-
365
- public class TaskEntry implements BaseColumns {
366
-
367
- public static final String TABLE = "tasks";
368
-
369
- public static final String COL_TASK_TITLE = "title";
370
-
371
- }
372
-
373
- }
374
-
375
-
376
-
377
- ```ここに言語名を入力
378
-
379
- TaskDbHelper.java
380
-
381
- ```
382
-
383
- public class TaskDbHelper extends SQLiteOpenHelper {
384
-
385
-
386
-
387
- public TaskDbHelper(Context context) {
388
-
389
- super(context, TaskContract.DB_NAME, null, TaskContract.DB_VERSION);
390
-
391
- }
392
-
393
-
394
-
395
- @Override
396
-
397
- public void onCreate(SQLiteDatabase db) {
398
-
399
- String createTable = "CREATE TABLE " + TaskContract.TaskEntry.TABLE + " ( " +
400
-
401
- TaskContract.TaskEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
402
-
403
- TaskContract.TaskEntry.COL_TASK_TITLE + " TEXT NOT NULL);";
404
-
405
-
406
-
407
- db.execSQL(createTable);
408
-
409
- }
410
-
411
-
412
-
413
- @Override
414
-
415
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
416
-
417
- db.execSQL("DROP TABLE IF EXISTS " + TaskContract.TaskEntry.TABLE);
418
-
419
- onCreate(db);
420
-
421
- }
422
-
423
- }
424
436
 
425
437
 
426
438