質問編集履歴

1

詳細の記載

2016/10/13 10:17

投稿

edoooooo
edoooooo

スコア476

test CHANGED
File without changes
test CHANGED
@@ -6,7 +6,575 @@
6
6
 
7
7
 
8
8
 
9
+ 本気で学ぶAndroidアプリ開発入門 SDK5対応
10
+
11
+ のTODOリストアプリを作るコードです。
12
+
13
+
14
+
9
- 丸投げですみません。アドバイスをい。宜しくお願いします。
15
+ アドバイスをいたきたです。宜しくお願いします。
16
+
17
+ MainActivityです。
18
+
19
+ ```java
20
+
21
+ package com.yokmama.learn10.chapter06.lesson28;
22
+
23
+
24
+
25
+ import android.os.Bundle;
26
+
27
+ import android.support.v4.app.FragmentActivity;
28
+
29
+ import android.support.v7.app.AppCompatActivity;
30
+
31
+ import android.widget.FrameLayout;
32
+
33
+
34
+
35
+ import java.util.List;
36
+
37
+
38
+
39
+
40
+
41
+ public class MainActivity extends AppCompatActivity {
42
+
43
+
44
+
45
+ private List<Todo> mTodoList;
46
+
47
+
48
+
49
+ private boolean mIsTablet = false;
50
+
51
+
52
+
53
+ @Override
54
+
55
+ protected void onCreate(Bundle savedInstanceState) {
56
+
57
+ super.onCreate(savedInstanceState);
58
+
59
+ setContentView(R.layout.activity_main);
60
+
61
+
62
+
63
+ //ダミーデータ作成
64
+
65
+ mTodoList = Todo.addDummyItem();
66
+
67
+
68
+
69
+ //TODOリスト一覧を表示
70
+
71
+ showTodoList();
72
+
73
+
74
+
75
+ //タブレットレイアウトなら右側にフォーム画面を表示
76
+
77
+ FrameLayout container2 = (FrameLayout) findViewById(R.id.container2);
78
+
79
+ if (container2 != null) {
80
+
81
+ mIsTablet = true;
82
+
83
+ showTodoForm(mTodoList.get(0));
84
+
85
+ }
86
+
87
+ }
88
+
89
+
90
+
91
+ @Override
92
+
93
+ public void onBackPressed() {
94
+
95
+ if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
96
+
97
+ //フォーム画面を開いている場合は画面を閉じる
98
+
99
+ getSupportFragmentManager().popBackStack();
100
+
101
+ } else {
102
+
103
+ //リスト画面の場合は通常のバックキー処理(アプリを終了)
104
+
105
+ super.onBackPressed();
106
+
107
+ }
108
+
109
+ }
110
+
111
+
112
+
113
+ /**
114
+
115
+ * TODOリスト一覧を表示
116
+
117
+ */
118
+
119
+ public void showTodoList() {
120
+
121
+ String tag = TodoListFragment.TAG;
122
+
123
+ getSupportFragmentManager().beginTransaction().replace(R.id.container,
124
+
125
+ TodoListFragment.newInstance(), tag).commit();
126
+
127
+ }
128
+
129
+
130
+
131
+ /**
132
+
133
+ * TODOフォーム画面を表示
134
+
135
+ *
136
+
137
+ * @param item TODOリストデータ
138
+
139
+ */
140
+
141
+ public void showTodoForm(Todo item) {
142
+
143
+ String tag = TodoFormFragment.TAG;
144
+
145
+ TodoFormFragment fragment;
146
+
147
+ if (item == null) {
148
+
149
+ fragment = TodoFormFragment.newInstance();
150
+
151
+ } else {
152
+
153
+ fragment = TodoFormFragment.newInstance(item.getColorLabel(),
154
+
155
+ item.getValue(), item.getCreatedTime());
156
+
157
+ }
158
+
159
+ if (!mIsTablet) {
160
+
161
+ //スマートフォンレイアウトの場合はcontainerに表示
162
+
163
+ getSupportFragmentManager().beginTransaction().replace(R.id.container,
164
+
165
+ fragment, tag).addToBackStack(tag).commit();
166
+
167
+ }else{
168
+
169
+ //タブレットレイアウトの場合はcontainer2に表示
170
+
171
+ getSupportFragmentManager().beginTransaction().replace(R.id.container2,
172
+
173
+ fragment, tag).addToBackStack(tag).commit();
174
+
175
+ }
176
+
177
+ }
178
+
179
+
180
+
181
+ public List<Todo> getTodoList() {
182
+
183
+ return mTodoList;
184
+
185
+ }
186
+
187
+
188
+
189
+ /**
190
+
191
+ * タブレットか判定.
192
+
193
+ * @return
194
+
195
+ */
196
+
197
+ public boolean isTablet() {
198
+
199
+ return mIsTablet;
200
+
201
+ }
202
+
203
+ }
204
+
205
+
206
+
207
+
208
+
209
+
210
+
211
+ ```
212
+
213
+ TODO.javaです。
214
+
215
+ ```java
216
+
217
+ package com.yokmama.learn10.chapter06.lesson28;
218
+
219
+
220
+
221
+ import java.util.ArrayList;
222
+
223
+ import java.util.List;
224
+
225
+
226
+
227
+ /**
228
+
229
+ * Created by m_iwasaki on 15/03/12.
230
+
231
+ */
232
+
233
+ public class
234
+
235
+
236
+
237
+
238
+
239
+ Todo {
240
+
241
+
242
+
243
+ private int colorLabel;
244
+
245
+
246
+
247
+ private long createdTime;
248
+
249
+
250
+
251
+ private String value;
252
+
253
+
254
+
255
+ public static interface ColorLabel {
256
+
257
+
258
+
259
+ public static final int NONE = 1;
260
+
261
+ public static final int PINK = 2;
262
+
263
+ public static final int INDIGO = 3;
264
+
265
+ public static final int GREEN = 4;
266
+
267
+ public static final int AMBER = 5;
268
+
269
+ }
270
+
271
+
272
+
273
+ public Todo(int colorLabel, String value, long createdTime) {
274
+
275
+ this.colorLabel = colorLabel;
276
+
277
+ this.value = value;
278
+
279
+ this.createdTime = createdTime;
280
+
281
+ }
282
+
283
+
284
+
285
+ public int getColorLabel() {
286
+
287
+ return colorLabel;
288
+
289
+ }
290
+
291
+
292
+
293
+ public void setColorLabel(int colorLabel) {
294
+
295
+ this.colorLabel = colorLabel;
296
+
297
+ }
298
+
299
+
300
+
301
+ public String getValue() {
302
+
303
+ return value;
304
+
305
+ }
306
+
307
+
308
+
309
+ public void setValue(String value) {
310
+
311
+ this.value = value;
312
+
313
+ }
314
+
315
+
316
+
317
+ public long getCreatedTime() {
318
+
319
+ return createdTime;
320
+
321
+ }
322
+
323
+
324
+
325
+ public void setCreatedTime(long createdTime) {
326
+
327
+ this.createdTime = createdTime;
328
+
329
+ }
330
+
331
+
332
+
333
+ /**
334
+
335
+ * テスト表示用にダミーのリストアイテムを作成.
336
+
337
+ */
338
+
339
+ public static List<Todo> addDummyItem() {
340
+
341
+ List<Todo> items = new ArrayList<>();
342
+
343
+ items.add(new Todo(Todo.ColorLabel.INDIGO, "猫に小判", System.currentTimeMillis() + 1));
344
+
345
+ items.add(new Todo(Todo.ColorLabel.PINK, "猫の手も借りたい", System.currentTimeMillis() + 2));
346
+
347
+ items.add(new Todo(Todo.ColorLabel.GREEN, "窮鼠猫を噛む", System.currentTimeMillis() + 3));
348
+
349
+ items.add(new Todo(Todo.ColorLabel.AMBER,
350
+
351
+ "猫は三年飼っても三日で恩を忘れる", System.currentTimeMillis() + 4));
352
+
353
+ items.add(new Todo(Todo.ColorLabel.NONE, "猫も杓子も", System.currentTimeMillis() + 5));
354
+
355
+ return items;
356
+
357
+ }
358
+
359
+ }
360
+
361
+ ```
362
+
363
+
364
+
365
+ TODOListAdapter.javaです。
366
+
367
+ ```java
368
+
369
+ package com.yokmama.learn10.chapter06.lesson28;
370
+
371
+
372
+
373
+ import android.content.Context;
374
+
375
+ import android.text.TextUtils;
376
+
377
+ import android.view.LayoutInflater;
378
+
379
+ import android.view.View;
380
+
381
+ import android.view.ViewGroup;
382
+
383
+ import android.widget.ArrayAdapter;
384
+
385
+ import android.widget.TextView;
386
+
387
+
388
+
389
+ import java.text.SimpleDateFormat;
390
+
391
+ import java.util.Date;
392
+
393
+ import java.util.List;
394
+
395
+
396
+
397
+ public class TodoListAdapter extends ArrayAdapter<Todo> {
398
+
399
+
400
+
401
+ /** フィールド */
402
+
403
+ private LayoutInflater mInflator;
404
+
405
+
406
+
407
+ public TodoListAdapter(Context context, List<Todo> objects) {
408
+
409
+ super(context, 0, objects);
410
+
411
+ mInflator = (LayoutInflater) context
412
+
413
+ .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
414
+
415
+ }
416
+
417
+
418
+
419
+ @Override
420
+
421
+ public View getView(final int position, View convertView, ViewGroup parent) {
422
+
423
+ ViewHolder holder;
424
+
425
+ if (convertView == null) {
426
+
427
+ convertView = mInflator.inflate(R.layout.item_list_row, parent, false);
428
+
429
+ holder = new ViewHolder();
430
+
431
+ holder.tvColorLabel = (TextView) convertView.findViewById(R.id.color_label);
432
+
433
+ holder.tvValue = (TextView) convertView.findViewById(R.id.value);
434
+
435
+ holder.tvCreatedTime = (TextView) convertView.findViewById(R.id.created_time);
436
+
437
+ convertView.setTag(holder);
438
+
439
+ } else {
440
+
441
+ holder = (ViewHolder) convertView.getTag();
442
+
443
+ }
444
+
445
+
446
+
447
+ //値をセット
448
+
449
+ String value = getItem(position).getValue();
450
+
451
+ if (!TextUtils.isEmpty(value)) {
452
+
453
+ holder.tvValue.setText(value);
454
+
455
+ }
456
+
457
+
458
+
459
+ //カラーラベルをセット
460
+
461
+ int color = getItem(position).getColorLabel();
462
+
463
+ holder.tvColorLabel.setBackgroundResource(getColorLabelResource(color));
464
+
465
+ if (!TextUtils.isEmpty(value)) {
466
+
467
+ holder.tvColorLabel.setText(value.substring(0, 1));
468
+
469
+ }
470
+
471
+
472
+
473
+ //日付をセット
474
+
475
+ String createdtime = getCreatedTime(getItem(position).getCreatedTime());
476
+
477
+ if (!TextUtils.isEmpty(createdtime)) {
478
+
479
+ holder.tvCreatedTime.setText(createdtime);
480
+
481
+ }
482
+
483
+
484
+
485
+ return convertView;
486
+
487
+ }
488
+
489
+
490
+
491
+ /**
492
+
493
+ * 現在年月をDate型返却.
494
+
495
+ */
496
+
497
+ public static String getCreatedTime(long createdTime) {
498
+
499
+ Date date = new Date(createdTime);
500
+
501
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/HH:mm");
502
+
503
+ return sdf.format(date);
504
+
505
+ }
506
+
507
+
508
+
509
+ /**
510
+
511
+ * カラーラベルのdrawableリソースIDを返却.
512
+
513
+ *
514
+
515
+ * @param color : カラー
516
+
517
+ */
518
+
519
+ private int getColorLabelResource(int color) {
520
+
521
+ int resId = R.drawable.bg_colorlabel_grey;
522
+
523
+ switch (color) {
524
+
525
+ case Todo.ColorLabel.PINK:
526
+
527
+ resId = R.drawable.bg_colorlabel_pink;
528
+
529
+ break;
530
+
531
+ case Todo.ColorLabel.INDIGO:
532
+
533
+ resId = R.drawable.bg_colorlabel_indigo;
534
+
535
+ break;
536
+
537
+ case Todo.ColorLabel.GREEN:
538
+
539
+ resId = R.drawable.bg_colorlabel_green;
540
+
541
+ break;
542
+
543
+ case Todo.ColorLabel.AMBER:
544
+
545
+ resId = R.drawable.bg_colorlabel_amber;
546
+
547
+ break;
548
+
549
+ }
550
+
551
+ return resId;
552
+
553
+ }
554
+
555
+
556
+
557
+ private class ViewHolder {
558
+
559
+
560
+
561
+ TextView tvColorLabel;
562
+
563
+
564
+
565
+ TextView tvCreatedTime;
566
+
567
+
568
+
569
+ TextView tvValue;
570
+
571
+ }
572
+
573
+ }
574
+
575
+
576
+
577
+
10
578
 
11
579
 
12
580
 
@@ -15,149 +583,3 @@
15
583
  ```java
16
584
 
17
585
  lesson28 E/Trace: error opening trace file: No such file or directory (2)
18
-
19
- E/AndroidRuntime: FATAL EXCEPTION: main
20
-
21
- E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yokmama.learn10.chapter06.lesson28/com.yokmama.learn10.chapter06.lesson28.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class FragmentLayout
22
-
23
- E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2070)
24
-
25
- E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095)
26
-
27
- /AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:137)
28
-
29
- /AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206)
30
-
31
- /AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99)
32
-
33
- /AndroidRuntime: at android.os.Looper.loop(Looper.java:213)
34
-
35
- /AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4786)
36
-
37
- /AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
38
-
39
- /AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511)
40
-
41
- ¥/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
42
-
43
- /AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
44
-
45
- /AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
46
-
47
- /AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class FragmentLayout
48
-
49
- /AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
50
-
51
- /AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
52
-
53
- /AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
54
-
55
- /AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
56
-
57
- /AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
58
-
59
- /AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
60
-
61
- /AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
62
-
63
- /AndroidRuntime: at com.yokmama.learn10.chapter06.lesson28.MainActivity.onCreate(MainActivity.java:20)
64
-
65
- /AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5008)
66
-
67
- /AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
68
-
69
- AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
70
-
71
- /AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095) 
72
-
73
- /AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:137) 
74
-
75
- /AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206) 
76
-
77
- /AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 
78
-
79
- /AndroidRuntime: at android.os.Looper.loop(Looper.java:213) 
80
-
81
- /AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4786) 
82
-
83
- /AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
84
-
85
- /AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 
86
-
87
- /AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
88
-
89
- /AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
90
-
91
- /AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 
92
-
93
- /AndroidRuntime: Caused by: java.lang.ClassNotFoundException: android.view.FragmentLayout
94
-
95
- /AndroidRuntime: at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
96
-
97
- /AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
98
-
99
- /AndroidRuntime: at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
100
-
101
- /AndroidRuntime: at android.view.LayoutInflater.createView(LayoutInflater.java:552)
102
-
103
- /AndroidRuntime: at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
104
-
105
- /AndroidRuntime: at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
106
-
107
- /AndroidRuntime: at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
108
-
109
- /AndroidRuntime: at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
110
-
111
- /AndroidRuntime: at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) 
112
-
113
- /AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 
114
-
115
- /AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 
116
-
117
- /AndroidRuntime: at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 
118
-
119
- /AndroidRuntime: at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249) 
120
-
121
- /AndroidRuntime: at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106) 
122
-
123
- /AndroidRuntime: at com.yokmama.learn10.chapter06.lesson28.MainActivity.onCreate(MainActivity.java:20) 
124
-
125
- AndroidRuntime: at android.app.Activity.performCreate(Activity.java:5008) 
126
-
127
- /AndroidRuntime: at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
128
-
129
- /AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034) 
130
-
131
- /AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2095) 
132
-
133
- /AndroidRuntime: at android.app.ActivityThread.access$600(ActivityThread.java:137) 
134
-
135
- /AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1206) 
136
-
137
- /AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:99) 
138
-
139
- /AndroidRuntime: at android.os.Looper.loop(Looper.java:213) 
140
-
141
- /AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:4786) 
142
-
143
- /AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 
144
-
145
- /AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:511) 
146
-
147
- /AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
148
-
149
- /AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 
150
-
151
- /AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 
152
-
153
- I/Process: Sending signal. PID: 5630 SIG: 9
154
-
155
-
156
-
157
-
158
-
159
- ```
160
-
161
- 字数制限にかからないように余分なファイル名と時間を削除しました。
162
-
163
- サンプルコードには、いろいろな機能が付いていて、クラスのjavaコードのどれを乗せれば位いいのかもわかりません。