質問編集履歴

3

誤記修正

2020/10/22 05:56

投稿

riria
riria

スコア1

test CHANGED
File without changes
test CHANGED
@@ -402,7 +402,7 @@
402
402
 
403
403
 
404
404
 
405
- MyAdapter.java
405
+ MyAdapter.java
406
406
 
407
407
  ```java
408
408
 
@@ -486,7 +486,7 @@
486
486
 
487
487
 
488
488
 
489
- Navigator.java
489
+ Navigator.java
490
490
 
491
491
  ```JAVA
492
492
 
@@ -502,9 +502,9 @@
502
502
 
503
503
  }
504
504
 
505
-
505
+ ```
506
-
506
+
507
- Model
507
+ Model.java
508
508
 
509
509
  ```Java
510
510
 
@@ -529,3 +529,5 @@
529
529
  }
530
530
 
531
531
  }
532
+
533
+ ```

2

誤記修正

2020/10/22 05:56

投稿

riria
riria

スコア1

test CHANGED
File without changes
test CHANGED
@@ -36,6 +36,8 @@
36
36
 
37
37
  <?xml version="1.0" encoding="utf-8"?>
38
38
 
39
+
40
+
39
41
  <layout>
40
42
 
41
43
  <data>
@@ -44,161 +46,223 @@
44
46
 
45
47
  name="ViewModel"
46
48
 
47
- type="com.websarva.wing.android.trylistviewbydatebinding.MainViewModel" />
49
+ type="com.websarva.wing.android.trylistviewadaptermvvm.MainViewModel" />
48
50
 
49
51
  </data>
50
52
 
51
- <LinearLayout
52
-
53
- xmlns:android="http://schemas.android.com/apk/res/android"
54
-
55
- android:orientation="vertical"
56
-
57
- android:layout_width="match_parent"
58
-
59
- android:layout_height="match_parent">
60
-
61
-
62
-
63
53
  <LinearLayout
64
54
 
65
- android:orientation="horizontal"
55
+ android:id="@+id/container"
56
+
57
+ xmlns:android="http://schemas.android.com/apk/res/android"
58
+
59
+ xmlns:tools="http://schemas.android.com/tools"
66
60
 
67
61
  android:layout_width="match_parent"
68
62
 
69
63
  android:layout_height="match_parent"
70
64
 
71
- android:onClick="@{()->ViewModel.onClicked()}">
65
+ tools:context=".MainActivity">
72
-
73
-
74
-
66
+
67
+
68
+
69
+
70
+
75
- <TextView
71
+ <ListView
72
+
73
+ android:layout_width="wrap_content"
74
+
75
+ android:layout_height="wrap_content"
76
+
77
+ android:id="@+id/listView"/>
78
+
79
+
80
+
81
+
82
+
83
+ </LinearLayout>
84
+
85
+ </layout>
86
+
87
+ ```
88
+
89
+
90
+
91
+ ↓foodrow.xml
92
+
93
+ ```xml
94
+
95
+ <?xml version="1.0" encoding="utf-8"?>
96
+
97
+ <layout>
98
+
99
+ <data>
100
+
101
+ <variable
102
+
103
+ name="ViewModel"
104
+
105
+ type="com.websarva.wing.android.trylistviewadaptermvvm.MainViewModel" />
106
+
107
+ </data>
108
+
109
+ <LinearLayout
110
+
111
+ xmlns:android="http://schemas.android.com/apk/res/android"
112
+
113
+ android:orientation="vertical"
114
+
115
+ android:layout_width="match_parent"
116
+
117
+ android:layout_height="match_parent">
118
+
119
+
120
+
121
+ <LinearLayout
122
+
123
+ android:orientation="horizontal"
76
124
 
77
125
  android:layout_width="match_parent"
78
126
 
127
+ android:layout_height="match_parent"
128
+
129
+ android:onClick="@{()->ViewModel.onClicked()}">
130
+
131
+
132
+
133
+ <TextView
134
+
135
+ android:layout_width="wrap_content"
136
+
79
- android:layout_height="wrap_content"
137
+ android:layout_height="wrap_content"
80
-
138
+
81
- android:text="@{ViewModel.name ,default = v3月14日}"
139
+ android:text="@{ViewModel.header ,default = v3月14日}"
82
-
140
+
83
- android:id="@+id/name"
141
+ android:id="@+id/name"
84
-
142
+
85
- android:textSize="30dp"
143
+ android:textSize="30dp"
86
-
144
+
87
- android:layout_weight="2" />
145
+ android:layout_weight="2" />
88
-
89
-
90
-
146
+
147
+
148
+
91
- <TextView
149
+ <TextView
92
-
150
+
93
- android:layout_width="match_parent"
151
+ android:layout_width="wrap_content"
94
-
152
+
95
- android:layout_height="wrap_content"
153
+ android:layout_height="wrap_content"
96
-
154
+
97
- android:text="@{ViewModel.price ,default = v3月14日}"
155
+ android:text="@{ViewModel.desc ,default = v3月14日}"
98
-
156
+
99
- android:id="@+id/price"
157
+ android:id="@+id/price"
100
-
158
+
101
- android:textSize="30dp"
159
+ android:textSize="30dp"
102
-
160
+
103
- android:layout_weight="1" />
161
+ android:layout_weight="1" />
162
+
163
+ </LinearLayout>
164
+
165
+
104
166
 
105
167
  </LinearLayout>
106
168
 
107
-
108
-
109
- </LinearLayout>
110
-
111
169
  </layout>
112
170
 
113
171
  ```
114
172
 
115
173
 
116
174
 
117
- foodrow.xml
118
-
119
- ```xml
120
-
121
- <?xml version="1.0" encoding="utf-8"?>
122
-
123
- <layout>
124
-
125
- <data>
126
-
127
- <variable
128
-
129
- name="ViewModel"
130
-
131
- type="com.websarva.wing.android.trylistviewadaptermvvm.MainViewModel" />
132
-
133
- </data>
134
-
135
- <LinearLayout
136
-
137
- xmlns:android="http://schemas.android.com/apk/res/android"
138
-
139
- android:orientation="vertical"
140
-
141
- android:layout_width="match_parent"
142
-
143
- android:layout_height="match_parent">
144
-
145
-
146
-
147
- <LinearLayout
148
-
149
- android:orientation="horizontal"
150
-
151
- android:layout_width="match_parent"
152
-
153
- android:layout_height="match_parent"
154
-
155
- android:onClick="@{()->ViewModel.onClicked()}">
156
-
157
-
158
-
159
- <TextView
160
-
161
- android:layout_width="wrap_content"
162
-
163
- android:layout_height="wrap_content"
164
-
165
- android:text="@{ViewModel.header ,default = v3月14日}"
166
-
167
- android:id="@+id/name"
168
-
169
- android:textSize="30dp"
170
-
171
- android:layout_weight="2" />
172
-
173
-
174
-
175
- <TextView
176
-
177
- android:layout_width="wrap_content"
178
-
179
- android:layout_height="wrap_content"
180
-
181
- android:text="@{ViewModel.desc ,default = v3月14日}"
182
-
183
- android:id="@+id/price"
184
-
185
- android:textSize="30dp"
186
-
187
- android:layout_weight="1" />
188
-
189
- </LinearLayout>
190
-
191
-
192
-
193
- </LinearLayout>
194
-
195
- </layout>
175
+ MainActivity.java
176
+
177
+ ```java
178
+
179
+ package com.websarva.wing.android.trylistviewadaptermvvm;
180
+
181
+
182
+
183
+ import androidx.appcompat.app.AppCompatActivity;
184
+
185
+ import androidx.databinding.DataBindingUtil;
186
+
187
+
188
+
189
+ import android.content.Intent;
190
+
191
+ import android.os.Bundle;
192
+
193
+
194
+
195
+ import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBinding;
196
+
197
+ import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBindingImpl;
198
+
199
+
200
+
201
+ import java.util.ArrayList;
202
+
203
+
204
+
205
+ public class MainActivity extends AppCompatActivity implements Navigator {
206
+
207
+ private ActivityMainBinding activityMainBinding;
208
+
209
+ private MainViewModel mainViewModel;
210
+
211
+ private ArrayList<MainViewModel> mainViewModelArrayList;
212
+
213
+ private MyAdapter myAdapter;
214
+
215
+
216
+
217
+
218
+
219
+ @Override
220
+
221
+ protected void onCreate(Bundle savedInstanceState) {
222
+
223
+ super.onCreate(savedInstanceState);
224
+
225
+ //setContentView(R.layout.activity_main);
226
+
227
+ activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
228
+
229
+ mainViewModel= new MainViewModel();
230
+
231
+ mainViewModel.setNavigator(this);
232
+
233
+ mainViewModelArrayList = mainViewModel.getArrayList();
234
+
235
+ myAdapter = new MyAdapter(this, mainViewModelArrayList);
236
+
237
+ activityMainBinding.listView.setAdapter(myAdapter);
238
+
239
+ //mainFragment.setViewModel(mainViewModel);
240
+
241
+ }
242
+
243
+
244
+
245
+ @Override
246
+
247
+ public void onGoEditRecord() {
248
+
249
+ Intent intent = new Intent(this, Bottom.class);
250
+
251
+ startActivity(intent);
252
+
253
+ }
254
+
255
+ }
256
+
257
+
258
+
259
+
196
260
 
197
261
  ```
198
262
 
199
263
 
200
264
 
201
- ↓MainActivity.java
265
+ ↓MainViewModel.java
202
266
 
203
267
  ```java
204
268
 
@@ -206,21 +270,167 @@
206
270
 
207
271
 
208
272
 
273
+ import androidx.lifecycle.MutableLiveData;
274
+
275
+
276
+
277
+ import java.util.ArrayList;
278
+
279
+
280
+
281
+ public class MainViewModel {
282
+
283
+
284
+
285
+ public String header;
286
+
287
+ public String desc;
288
+
289
+ public Navigator mNavigator;
290
+
291
+
292
+
293
+ public MainViewModel(Model model){
294
+
295
+ this.header = model.header;
296
+
297
+ this.desc = model.desc;
298
+
299
+
300
+
301
+ }
302
+
303
+
304
+
305
+ public MainViewModel() {
306
+
307
+ }
308
+
309
+
310
+
311
+
312
+
313
+ public void setNavigator(Navigator navigator) {
314
+
315
+ this.mNavigator = navigator;
316
+
317
+
318
+
319
+ }
320
+
321
+
322
+
323
+ public String getHeader() {
324
+
325
+ return header;
326
+
327
+ }
328
+
329
+
330
+
331
+ public void setHeader(String header) {
332
+
333
+ this.header = header;
334
+
335
+ }
336
+
337
+
338
+
339
+ public String getDesc() {
340
+
341
+ return desc;
342
+
343
+ }
344
+
345
+
346
+
347
+ public void setDesc(String desc) {
348
+
349
+ this.desc = desc;
350
+
351
+ }
352
+
353
+
354
+
355
+ public ArrayList<MainViewModel> getArrayList(){
356
+
357
+ ArrayList<MainViewModel> modelArrayList = new ArrayList<>();
358
+
359
+ MainViewModel viewModel1 = new MainViewModel(new Model("りんご","100"));
360
+
361
+ MainViewModel viewModel2 = new MainViewModel(new Model("みかん","100"));
362
+
363
+ MainViewModel viewModel3 = new MainViewModel(new Model("いちご","100"));
364
+
365
+ MainViewModel viewModel4 = new MainViewModel(new Model("スイカ","100"));
366
+
367
+
368
+
369
+ modelArrayList.add(viewModel1);
370
+
371
+ modelArrayList.add(viewModel2);
372
+
373
+ modelArrayList.add(viewModel3);
374
+
375
+ modelArrayList.add(viewModel4);
376
+
377
+
378
+
379
+ return modelArrayList;
380
+
381
+
382
+
383
+ }
384
+
385
+
386
+
387
+ public void onClicked(){
388
+
389
+ System.out.println(mNavigator);
390
+
391
+ mNavigator.onGoEditRecord();
392
+
393
+ }
394
+
395
+ }
396
+
397
+
398
+
399
+
400
+
401
+ ```
402
+
403
+
404
+
405
+ MyAdapter.java
406
+
407
+ ```java
408
+
409
+ package com.websarva.wing.android.trylistviewadaptermvvm;
410
+
411
+
412
+
413
+ import android.content.Context;
414
+
415
+ import android.view.LayoutInflater;
416
+
417
+ import android.view.View;
418
+
419
+ import android.view.ViewGroup;
420
+
421
+ import android.widget.ArrayAdapter;
422
+
423
+
424
+
425
+ import androidx.annotation.NonNull;
426
+
209
- import androidx.appcompat.app.AppCompatActivity;
427
+ import androidx.annotation.Nullable;
210
428
 
211
429
  import androidx.databinding.DataBindingUtil;
212
430
 
213
431
 
214
432
 
215
- import android.content.Intent;
216
-
217
- import android.os.Bundle;
218
-
219
-
220
-
221
- import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBinding;
433
+ import com.websarva.wing.android.trylistviewadaptermvvm.databinding.FoodrowBinding;
222
-
223
- import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBindingImpl;
224
434
 
225
435
 
226
436
 
@@ -228,53 +438,43 @@
228
438
 
229
439
 
230
440
 
231
- public class MainActivity extends AppCompatActivity implements Navigator {
441
+ public class MyAdapter extends ArrayAdapter<MainViewModel> {
232
-
233
- private ActivityMainBinding activityMainBinding;
442
+
234
-
235
- private MainViewModel mainViewModel;
236
-
237
- private ArrayList<MainViewModel> mainViewModelArrayList;
443
+ private ArrayList<MainViewModel> modelArrayList;
238
-
444
+
239
- private MyAdapter myAdapter;
445
+ private Context context1;
446
+
447
+
448
+
240
-
449
+ public MyAdapter(@NonNull Context context, ArrayList<MainViewModel> modelArrayList){
450
+
241
-
451
+ super(context, R.layout.foodrow,modelArrayList);
452
+
242
-
453
+ this.context1 = context;
454
+
243
-
455
+ this.modelArrayList = modelArrayList;
456
+
457
+ }
458
+
459
+
460
+
461
+ @NonNull
244
462
 
245
463
  @Override
246
464
 
465
+ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
466
+
467
+
468
+
247
- protected void onCreate(Bundle savedInstanceState) {
469
+ LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
248
-
249
- super.onCreate(savedInstanceState);
470
+
250
-
251
- //setContentView(R.layout.activity_main);
252
-
253
- activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
471
+ FoodrowBinding foodrowBinding = DataBindingUtil.inflate(layoutInflater,R.layout.foodrow,parent,false);
254
-
255
- mainViewModel= new MainViewModel();
472
+
256
-
257
- mainViewModel.setNavigator(this);
258
-
259
- mainViewModelArrayList = mainViewModel.getArrayList();
473
+ foodrowBinding.setViewModel(modelArrayList.get(position));
260
-
261
- myAdapter = new MyAdapter(this, mainViewModelArrayList);
474
+
262
-
263
- activityMainBinding.listView.setAdapter(myAdapter);
475
+
264
-
265
- //mainFragment.setViewModel(mainViewModel);
476
+
266
-
267
- }
268
-
269
-
270
-
271
- @Override
272
-
273
- public void onGoEditRecord() {
477
+ return foodrowBinding.getRoot();
274
-
275
- Intent intent = new Intent(this, Bottom.class);
276
-
277
- startActivity(intent);
278
478
 
279
479
  }
280
480
 
@@ -282,276 +482,50 @@
282
482
 
283
483
 
284
484
 
285
-
286
-
287
485
  ```
288
486
 
289
487
 
290
488
 
291
- ↓MainViewModel.java
489
+ Navigator.java
292
-
490
+
293
- ```java
491
+ ```JAVA
294
492
 
295
493
  package com.websarva.wing.android.trylistviewadaptermvvm;
296
494
 
297
495
 
298
496
 
299
- import androidx.lifecycle.MutableLiveData;
497
+ public interface Navigator {
300
-
301
-
302
-
498
+
303
- import java.util.ArrayList;
499
+ void onGoEditRecord();
500
+
501
+
502
+
304
-
503
+ }
504
+
505
+
506
+
305
-
507
+ Model
508
+
306
-
509
+ ```Java
510
+
511
+ package com.websarva.wing.android.trylistviewadaptermvvm;
512
+
513
+
514
+
307
- public class MainViewModel {
515
+ public class Model {
308
-
309
-
310
516
 
311
517
  public String header;
312
518
 
313
519
  public String desc;
314
520
 
315
- public Navigator mNavigator;
521
+
316
-
317
-
318
-
319
- public MainViewModel(Model model){
522
+
320
-
321
- this.header = model.header;
322
-
323
- this.desc = model.desc;
324
-
325
-
326
-
327
- }
328
-
329
-
330
-
331
- public MainViewModel() {
332
-
333
- }
334
-
335
-
336
-
337
-
338
-
339
- public void setNavigator(Navigator navigator) {
340
-
341
- this.mNavigator = navigator;
342
-
343
-
344
-
345
- }
346
-
347
-
348
-
349
- public String getHeader() {
350
-
351
- return header;
352
-
353
- }
354
-
355
-
356
-
357
- public void setHeader(String header) {
523
+ public Model(String header, String desc) {
358
524
 
359
525
  this.header = header;
360
526
 
361
- }
362
-
363
-
364
-
365
- public String getDesc() {
366
-
367
- return desc;
368
-
369
- }
370
-
371
-
372
-
373
- public void setDesc(String desc) {
374
-
375
527
  this.desc = desc;
376
528
 
377
529
  }
378
530
 
379
-
380
-
381
- public ArrayList<MainViewModel> getArrayList(){
382
-
383
- ArrayList<MainViewModel> modelArrayList = new ArrayList<>();
384
-
385
- MainViewModel viewModel1 = new MainViewModel(new Model("りんご","100"));
386
-
387
- MainViewModel viewModel2 = new MainViewModel(new Model("みかん","100"));
388
-
389
- MainViewModel viewModel3 = new MainViewModel(new Model("いちご","100"));
390
-
391
- MainViewModel viewModel4 = new MainViewModel(new Model("スイカ","100"));
392
-
393
-
394
-
395
- modelArrayList.add(viewModel1);
396
-
397
- modelArrayList.add(viewModel2);
398
-
399
- modelArrayList.add(viewModel3);
400
-
401
- modelArrayList.add(viewModel4);
402
-
403
-
404
-
405
- return modelArrayList;
406
-
407
-
408
-
409
- }
410
-
411
-
412
-
413
- public void onClicked(){
414
-
415
- System.out.println(mNavigator);
416
-
417
- mNavigator.onGoEditRecord();
418
-
419
- }
420
-
421
531
  }
422
-
423
-
424
-
425
-
426
-
427
- ```
428
-
429
-
430
-
431
- MyAdapter.java
432
-
433
- ```java
434
-
435
- package com.websarva.wing.android.trylistviewadaptermvvm;
436
-
437
-
438
-
439
- import android.content.Context;
440
-
441
- import android.view.LayoutInflater;
442
-
443
- import android.view.View;
444
-
445
- import android.view.ViewGroup;
446
-
447
- import android.widget.ArrayAdapter;
448
-
449
-
450
-
451
- import androidx.annotation.NonNull;
452
-
453
- import androidx.annotation.Nullable;
454
-
455
- import androidx.databinding.DataBindingUtil;
456
-
457
-
458
-
459
- import com.websarva.wing.android.trylistviewadaptermvvm.databinding.FoodrowBinding;
460
-
461
-
462
-
463
- import java.util.ArrayList;
464
-
465
-
466
-
467
- public class MyAdapter extends ArrayAdapter<MainViewModel> {
468
-
469
- private ArrayList<MainViewModel> modelArrayList;
470
-
471
- private Context context1;
472
-
473
-
474
-
475
- public MyAdapter(@NonNull Context context, ArrayList<MainViewModel> modelArrayList){
476
-
477
- super(context, R.layout.foodrow,modelArrayList);
478
-
479
- this.context1 = context;
480
-
481
- this.modelArrayList = modelArrayList;
482
-
483
- }
484
-
485
-
486
-
487
- @NonNull
488
-
489
- @Override
490
-
491
- public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
492
-
493
-
494
-
495
- LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
496
-
497
- FoodrowBinding foodrowBinding = DataBindingUtil.inflate(layoutInflater,R.layout.foodrow,parent,false);
498
-
499
- foodrowBinding.setViewModel(modelArrayList.get(position));
500
-
501
-
502
-
503
- return foodrowBinding.getRoot();
504
-
505
- }
506
-
507
- }
508
-
509
-
510
-
511
- ```
512
-
513
-
514
-
515
- Navigator.java
516
-
517
- ```JAVA
518
-
519
- package com.websarva.wing.android.trylistviewadaptermvvm;
520
-
521
-
522
-
523
- public interface Navigator {
524
-
525
- void onGoEditRecord();
526
-
527
-
528
-
529
- }
530
-
531
-
532
-
533
- Model
534
-
535
- ```Java
536
-
537
- package com.websarva.wing.android.trylistviewadaptermvvm;
538
-
539
-
540
-
541
- public class Model {
542
-
543
- public String header;
544
-
545
- public String desc;
546
-
547
-
548
-
549
- public Model(String header, String desc) {
550
-
551
- this.header = header;
552
-
553
- this.desc = desc;
554
-
555
- }
556
-
557
- }

1

全てのコードをのせました。

2020/10/22 05:53

投稿

riria
riria

スコア1

test CHANGED
File without changes
test CHANGED
@@ -114,17 +114,349 @@
114
114
 
115
115
 
116
116
 
117
+ ↓foodrow.xml
118
+
119
+ ```xml
120
+
121
+ <?xml version="1.0" encoding="utf-8"?>
122
+
123
+ <layout>
124
+
125
+ <data>
126
+
127
+ <variable
128
+
129
+ name="ViewModel"
130
+
131
+ type="com.websarva.wing.android.trylistviewadaptermvvm.MainViewModel" />
132
+
133
+ </data>
134
+
135
+ <LinearLayout
136
+
137
+ xmlns:android="http://schemas.android.com/apk/res/android"
138
+
139
+ android:orientation="vertical"
140
+
141
+ android:layout_width="match_parent"
142
+
143
+ android:layout_height="match_parent">
144
+
145
+
146
+
147
+ <LinearLayout
148
+
149
+ android:orientation="horizontal"
150
+
151
+ android:layout_width="match_parent"
152
+
153
+ android:layout_height="match_parent"
154
+
155
+ android:onClick="@{()->ViewModel.onClicked()}">
156
+
157
+
158
+
159
+ <TextView
160
+
161
+ android:layout_width="wrap_content"
162
+
163
+ android:layout_height="wrap_content"
164
+
165
+ android:text="@{ViewModel.header ,default = v3月14日}"
166
+
167
+ android:id="@+id/name"
168
+
169
+ android:textSize="30dp"
170
+
171
+ android:layout_weight="2" />
172
+
173
+
174
+
175
+ <TextView
176
+
177
+ android:layout_width="wrap_content"
178
+
179
+ android:layout_height="wrap_content"
180
+
181
+ android:text="@{ViewModel.desc ,default = v3月14日}"
182
+
183
+ android:id="@+id/price"
184
+
185
+ android:textSize="30dp"
186
+
187
+ android:layout_weight="1" />
188
+
189
+ </LinearLayout>
190
+
191
+
192
+
193
+ </LinearLayout>
194
+
195
+ </layout>
196
+
197
+ ```
198
+
199
+
200
+
201
+ ↓MainActivity.java
202
+
203
+ ```java
204
+
205
+ package com.websarva.wing.android.trylistviewadaptermvvm;
206
+
207
+
208
+
209
+ import androidx.appcompat.app.AppCompatActivity;
210
+
211
+ import androidx.databinding.DataBindingUtil;
212
+
213
+
214
+
215
+ import android.content.Intent;
216
+
217
+ import android.os.Bundle;
218
+
219
+
220
+
221
+ import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBinding;
222
+
223
+ import com.websarva.wing.android.trylistviewadaptermvvm.databinding.ActivityMainBindingImpl;
224
+
225
+
226
+
227
+ import java.util.ArrayList;
228
+
229
+
230
+
231
+ public class MainActivity extends AppCompatActivity implements Navigator {
232
+
233
+ private ActivityMainBinding activityMainBinding;
234
+
235
+ private MainViewModel mainViewModel;
236
+
237
+ private ArrayList<MainViewModel> mainViewModelArrayList;
238
+
239
+ private MyAdapter myAdapter;
240
+
241
+
242
+
243
+
244
+
245
+ @Override
246
+
247
+ protected void onCreate(Bundle savedInstanceState) {
248
+
249
+ super.onCreate(savedInstanceState);
250
+
251
+ //setContentView(R.layout.activity_main);
252
+
253
+ activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
254
+
255
+ mainViewModel= new MainViewModel();
256
+
257
+ mainViewModel.setNavigator(this);
258
+
259
+ mainViewModelArrayList = mainViewModel.getArrayList();
260
+
261
+ myAdapter = new MyAdapter(this, mainViewModelArrayList);
262
+
263
+ activityMainBinding.listView.setAdapter(myAdapter);
264
+
265
+ //mainFragment.setViewModel(mainViewModel);
266
+
267
+ }
268
+
269
+
270
+
271
+ @Override
272
+
273
+ public void onGoEditRecord() {
274
+
275
+ Intent intent = new Intent(this, Bottom.class);
276
+
277
+ startActivity(intent);
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+
286
+
287
+ ```
288
+
289
+
290
+
117
291
  ↓MainViewModel.java
118
292
 
119
293
  ```java
120
294
 
121
- package com.websarva.wing.android.trylistviewbydatebinding;
295
+ package com.websarva.wing.android.trylistviewadaptermvvm;
296
+
297
+
298
+
122
-
299
+ import androidx.lifecycle.MutableLiveData;
300
+
301
+
302
+
123
-
303
+ import java.util.ArrayList;
304
+
305
+
306
+
307
+ public class MainViewModel {
308
+
309
+
310
+
311
+ public String header;
312
+
313
+ public String desc;
314
+
315
+ public Navigator mNavigator;
316
+
317
+
318
+
319
+ public MainViewModel(Model model){
320
+
321
+ this.header = model.header;
322
+
323
+ this.desc = model.desc;
324
+
325
+
326
+
327
+ }
328
+
329
+
330
+
331
+ public MainViewModel() {
332
+
333
+ }
334
+
335
+
336
+
337
+
338
+
339
+ public void setNavigator(Navigator navigator) {
340
+
341
+ this.mNavigator = navigator;
342
+
343
+
344
+
345
+ }
346
+
347
+
348
+
349
+ public String getHeader() {
350
+
351
+ return header;
352
+
353
+ }
354
+
355
+
356
+
357
+ public void setHeader(String header) {
358
+
359
+ this.header = header;
360
+
361
+ }
362
+
363
+
364
+
365
+ public String getDesc() {
366
+
367
+ return desc;
368
+
369
+ }
370
+
371
+
372
+
373
+ public void setDesc(String desc) {
374
+
375
+ this.desc = desc;
376
+
377
+ }
378
+
379
+
380
+
381
+ public ArrayList<MainViewModel> getArrayList(){
382
+
383
+ ArrayList<MainViewModel> modelArrayList = new ArrayList<>();
384
+
385
+ MainViewModel viewModel1 = new MainViewModel(new Model("りんご","100"));
386
+
387
+ MainViewModel viewModel2 = new MainViewModel(new Model("みかん","100"));
388
+
389
+ MainViewModel viewModel3 = new MainViewModel(new Model("いちご","100"));
390
+
391
+ MainViewModel viewModel4 = new MainViewModel(new Model("スイカ","100"));
392
+
393
+
394
+
395
+ modelArrayList.add(viewModel1);
396
+
397
+ modelArrayList.add(viewModel2);
398
+
399
+ modelArrayList.add(viewModel3);
400
+
401
+ modelArrayList.add(viewModel4);
402
+
403
+
404
+
405
+ return modelArrayList;
406
+
407
+
408
+
409
+ }
410
+
411
+
412
+
413
+ public void onClicked(){
414
+
415
+ System.out.println(mNavigator);
416
+
417
+ mNavigator.onGoEditRecord();
418
+
419
+ }
420
+
421
+ }
422
+
423
+
424
+
425
+
426
+
427
+ ```
428
+
429
+
430
+
431
+ MyAdapter.java
432
+
433
+ ```java
434
+
435
+ package com.websarva.wing.android.trylistviewadaptermvvm;
436
+
437
+
438
+
439
+ import android.content.Context;
440
+
441
+ import android.view.LayoutInflater;
124
442
 
125
443
  import android.view.View;
126
444
 
445
+ import android.view.ViewGroup;
446
+
127
- import android.widget.AdapterView;
447
+ import android.widget.ArrayAdapter;
448
+
449
+
450
+
451
+ import androidx.annotation.NonNull;
452
+
453
+ import androidx.annotation.Nullable;
454
+
455
+ import androidx.databinding.DataBindingUtil;
456
+
457
+
458
+
459
+ import com.websarva.wing.android.trylistviewadaptermvvm.databinding.FoodrowBinding;
128
460
 
129
461
 
130
462
 
@@ -132,277 +464,65 @@
132
464
 
133
465
 
134
466
 
135
- public class MainViewModel {
136
-
137
- public String name;
138
-
139
- public String price;
140
-
141
- public long id;
142
-
143
- ArrayList<MainViewModel> modelArrayList = new ArrayList<>();
144
-
145
-
146
-
147
- public MainActivity mainActivity;
148
-
149
- public MainViewModel(Repository repository){
150
-
151
- this.name = repository.name;
152
-
153
- this.price = repository.price;
154
-
155
-
156
-
157
- }
158
-
159
-
160
-
161
- public MainViewModel() {
162
-
163
- }
164
-
165
-
166
-
167
- public long getId() {
168
-
169
- return id;
170
-
171
- }
172
-
173
-
174
-
175
- public void setId(long id) {
176
-
177
- this.id = id;
178
-
179
- }
180
-
181
-
182
-
183
-
184
-
185
- public String getName() {
186
-
187
- return name;
188
-
189
- }
190
-
191
-
192
-
193
- public void setName(String name) {
194
-
195
- this.name = name;
196
-
197
- }
198
-
199
-
200
-
201
- public String getPrice() {
202
-
203
- return price;
204
-
205
- }
206
-
207
-
208
-
209
- public void setPrice(String price) {
210
-
211
- this.price = price;
212
-
213
- }
214
-
215
-
216
-
217
- public ArrayList<MainViewModel> getArrayList(){
218
-
219
- MainViewModel viewModel1 = new MainViewModel(new Repository("りんご","100"));
220
-
221
- MainViewModel viewModel2 = new MainViewModel(new Repository("みかん","100"));
222
-
223
- MainViewModel viewModel3 = new MainViewModel(new Repository("いちご","100"));
224
-
225
- MainViewModel viewModel4 = new MainViewModel(new Repository("スイカ","100"));
226
-
227
-
228
-
229
- modelArrayList.add(viewModel1);
230
-
231
- modelArrayList.add(viewModel2);
232
-
233
- modelArrayList.add(viewModel3);
234
-
235
- modelArrayList.add(viewModel4);
236
-
237
-
238
-
239
- return modelArrayList;
240
-
241
-
242
-
243
- }
244
-
245
-
246
-
247
- public void onClicked() {
248
-
249
- mainActivity.onGoEditRecord();
250
-
251
-
252
-
253
- }
254
-
255
-
256
-
257
-
467
+ public class MyAdapter extends ArrayAdapter<MainViewModel> {
468
+
469
+ private ArrayList<MainViewModel> modelArrayList;
470
+
471
+ private Context context1;
472
+
473
+
474
+
475
+ public MyAdapter(@NonNull Context context, ArrayList<MainViewModel> modelArrayList){
476
+
477
+ super(context, R.layout.foodrow,modelArrayList);
478
+
479
+ this.context1 = context;
480
+
481
+ this.modelArrayList = modelArrayList;
482
+
483
+ }
484
+
485
+
486
+
487
+ @NonNull
488
+
489
+ @Override
490
+
491
+ public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
492
+
493
+
494
+
495
+ LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
496
+
497
+ FoodrowBinding foodrowBinding = DataBindingUtil.inflate(layoutInflater,R.layout.foodrow,parent,false);
498
+
499
+ foodrowBinding.setViewModel(modelArrayList.get(position));
500
+
501
+
502
+
503
+ return foodrowBinding.getRoot();
504
+
505
+ }
258
506
 
259
507
  }
260
508
 
509
+
510
+
261
511
  ```
262
512
 
263
513
 
264
514
 
265
- ↓MainActivity.java
515
+ Navigator.java
266
-
516
+
267
- ```java
517
+ ```JAVA
268
-
518
+
269
- package com.websarva.wing.android.trylistviewbydatebinding;
519
+ package com.websarva.wing.android.trylistviewadaptermvvm;
270
-
271
-
272
-
273
- import androidx.annotation.NonNull;
520
+
274
-
275
- import androidx.appcompat.app.AppCompatActivity;
521
+
276
-
277
- import androidx.databinding.DataBindingUtil;
522
+
278
-
279
- import androidx.fragment.app.Fragment;
280
-
281
- import androidx.fragment.app.FragmentManager;
282
-
283
- import androidx.fragment.app.FragmentTransaction;
284
-
285
-
286
-
287
- import android.content.Intent;
288
-
289
- import android.os.Bundle;
290
-
291
-
292
-
293
- import com.websarva.wing.android.trylistviewbydatebinding.databinding.ActivityMainBinding;
294
-
295
-
296
-
297
- import java.util.ArrayList;
298
-
299
-
300
-
301
- import static androidx.core.util.Preconditions.checkNotNull;
302
-
303
-
304
-
305
-
306
-
307
- public class MainActivity extends AppCompatActivity {
308
-
309
-
310
-
311
- private ActivityMainBinding activityMainBinding;
312
-
313
- private MainViewModel mainViewModel;
523
+ public interface Navigator {
314
-
315
- private MainFragment mainFragment;
524
+
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
- @Override
324
-
325
- protected void onCreate(Bundle savedInstanceState) {
326
-
327
- super.onCreate(savedInstanceState);
328
-
329
- //setContentView(R.layout.activity_main);
330
-
331
-
332
-
333
- System.out.println("appスタート?");
334
-
335
-
336
-
337
- activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
338
-
339
- // mainViewModel = new MainViewModel();
340
-
341
-
342
-
343
- mainFragment = new MainFragment();
344
-
345
- // mainFragment.setViewModel(mainViewModel);
346
-
347
- mainFragment = findOrCreateViewFragment();
348
-
349
-
350
-
351
- }
352
-
353
- @NonNull
354
-
355
- private MainFragment findOrCreateViewFragment() {
356
-
357
-
358
-
359
- mainFragment = (MainFragment) getSupportFragmentManager().findFragmentById(R.id.container);
360
-
361
- if (mainFragment == null) {
362
-
363
- mainFragment = MainFragment.newInstance();
364
-
365
- addFragmentToActivity(getSupportFragmentManager(),
366
-
367
- mainFragment, String.valueOf(R.id.container));
368
-
369
- }
370
-
371
- return mainFragment;
372
-
373
- }
374
-
375
- public static void addFragmentToActivity (@NonNull FragmentManager fragmentManager,
376
-
377
- @NonNull Fragment fragment, String tag) {
378
-
379
-
380
-
381
- //Fragmentの呼び出し
382
-
383
- FragmentTransaction transaction = fragmentManager.beginTransaction();
384
-
385
- transaction.add(fragment, tag);
386
-
387
- transaction.commit();
388
-
389
-
390
-
391
- }
392
-
393
-
394
-
395
- // @Override
396
-
397
- public void onGoEditRecord() {
525
+ void onGoEditRecord();
398
-
399
- Intent intent = new Intent(this, Bottom.class);
400
-
401
- startActivity(intent);
402
-
403
- }
404
-
405
-
406
526
 
407
527
 
408
528
 
@@ -410,4 +530,28 @@
410
530
 
411
531
 
412
532
 
533
+ Model
534
+
413
- ```
535
+ ```Java
536
+
537
+ package com.websarva.wing.android.trylistviewadaptermvvm;
538
+
539
+
540
+
541
+ public class Model {
542
+
543
+ public String header;
544
+
545
+ public String desc;
546
+
547
+
548
+
549
+ public Model(String header, String desc) {
550
+
551
+ this.header = header;
552
+
553
+ this.desc = desc;
554
+
555
+ }
556
+
557
+ }