質問編集履歴
3
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -387,127 +387,3 @@
|
|
387
387
|
}
|
388
388
|
|
389
389
|
```
|
390
|
-
|
391
|
-
MemoAdapter(修正後)
|
392
|
-
|
393
|
-
---
|
394
|
-
|
395
|
-
```ここに言語を入力
|
396
|
-
|
397
|
-
public class MemoAdapter extends RealmRecyclerViewAdapter<Memo, MemoAdapter.MemoViewHolder> {
|
398
|
-
|
399
|
-
private OrderedRealmCollection<Memo> mDataset;
|
400
|
-
|
401
|
-
private MemoListActivity memoListActivity;
|
402
|
-
|
403
|
-
private static final String TAG = "MemoAdapter";
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
//フラグメント遷移処理メソッドを持つアクティビティを設定
|
408
|
-
|
409
|
-
void setActivity(MemoListActivity mActivity) {
|
410
|
-
|
411
|
-
Log.d(TAG, "setActivity1 = " + memoListActivity);
|
412
|
-
|
413
|
-
this.memoListActivity = mActivity;
|
414
|
-
|
415
|
-
Log.d(TAG, "setActivity2 = " + memoListActivity);
|
416
|
-
|
417
|
-
}
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
MemoAdapter(OrderedRealmCollection<Memo> memoDataset) {
|
422
|
-
|
423
|
-
super(memoDataset, true);
|
424
|
-
|
425
|
-
this.mDataset = memoDataset;
|
426
|
-
|
427
|
-
setHasStableIds(true);
|
428
|
-
|
429
|
-
}
|
430
|
-
|
431
|
-
|
432
|
-
|
433
|
-
@Override
|
434
|
-
|
435
|
-
public MemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
436
|
-
|
437
|
-
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_memolist, parent, false);
|
438
|
-
|
439
|
-
return new MemoViewHolder(view);
|
440
|
-
|
441
|
-
}
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
@Override
|
446
|
-
|
447
|
-
public void onBindViewHolder(final MemoViewHolder holder, final int position){
|
448
|
-
|
449
|
-
final Memo obj = getItem(position);
|
450
|
-
|
451
|
-
holder.memo.setText(obj.getMemo());
|
452
|
-
|
453
|
-
holder.id.setText(obj.getId());
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
// アイテムクリックされたら編集画面へ
|
458
|
-
|
459
|
-
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
460
|
-
|
461
|
-
@Override
|
462
|
-
|
463
|
-
public void onClick(View v) {
|
464
|
-
|
465
|
-
final String id = obj.getId();
|
466
|
-
|
467
|
-
Log.d(TAG, "id onClick itemView = " + id);
|
468
|
-
|
469
|
-
Log.d(TAG, "onClick itemView = " + memoListActivity);
|
470
|
-
|
471
|
-
memoListActivity.replaceToEditMemoFragment(id);
|
472
|
-
|
473
|
-
}
|
474
|
-
|
475
|
-
});
|
476
|
-
|
477
|
-
}
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
@Override
|
482
|
-
|
483
|
-
public int getItemCount() {
|
484
|
-
|
485
|
-
return mDataset.size();
|
486
|
-
|
487
|
-
}
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
static class MemoViewHolder extends RecyclerView.ViewHolder {
|
492
|
-
|
493
|
-
TextView memo;
|
494
|
-
|
495
|
-
TextView id;
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
MemoViewHolder(View view) {
|
500
|
-
|
501
|
-
super(view);
|
502
|
-
|
503
|
-
memo = view.findViewById(R.id.memoContent);
|
504
|
-
|
505
|
-
id = view.findViewById(R.id.memoId);
|
506
|
-
|
507
|
-
}
|
508
|
-
|
509
|
-
}
|
510
|
-
|
511
|
-
}
|
512
|
-
|
513
|
-
```
|
2
コードを追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -387,3 +387,127 @@
|
|
387
387
|
}
|
388
388
|
|
389
389
|
```
|
390
|
+
|
391
|
+
MemoAdapter(修正後)
|
392
|
+
|
393
|
+
---
|
394
|
+
|
395
|
+
```ここに言語を入力
|
396
|
+
|
397
|
+
public class MemoAdapter extends RealmRecyclerViewAdapter<Memo, MemoAdapter.MemoViewHolder> {
|
398
|
+
|
399
|
+
private OrderedRealmCollection<Memo> mDataset;
|
400
|
+
|
401
|
+
private MemoListActivity memoListActivity;
|
402
|
+
|
403
|
+
private static final String TAG = "MemoAdapter";
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
//フラグメント遷移処理メソッドを持つアクティビティを設定
|
408
|
+
|
409
|
+
void setActivity(MemoListActivity mActivity) {
|
410
|
+
|
411
|
+
Log.d(TAG, "setActivity1 = " + memoListActivity);
|
412
|
+
|
413
|
+
this.memoListActivity = mActivity;
|
414
|
+
|
415
|
+
Log.d(TAG, "setActivity2 = " + memoListActivity);
|
416
|
+
|
417
|
+
}
|
418
|
+
|
419
|
+
|
420
|
+
|
421
|
+
MemoAdapter(OrderedRealmCollection<Memo> memoDataset) {
|
422
|
+
|
423
|
+
super(memoDataset, true);
|
424
|
+
|
425
|
+
this.mDataset = memoDataset;
|
426
|
+
|
427
|
+
setHasStableIds(true);
|
428
|
+
|
429
|
+
}
|
430
|
+
|
431
|
+
|
432
|
+
|
433
|
+
@Override
|
434
|
+
|
435
|
+
public MemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
|
436
|
+
|
437
|
+
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.listitem_memolist, parent, false);
|
438
|
+
|
439
|
+
return new MemoViewHolder(view);
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
|
444
|
+
|
445
|
+
@Override
|
446
|
+
|
447
|
+
public void onBindViewHolder(final MemoViewHolder holder, final int position){
|
448
|
+
|
449
|
+
final Memo obj = getItem(position);
|
450
|
+
|
451
|
+
holder.memo.setText(obj.getMemo());
|
452
|
+
|
453
|
+
holder.id.setText(obj.getId());
|
454
|
+
|
455
|
+
|
456
|
+
|
457
|
+
// アイテムクリックされたら編集画面へ
|
458
|
+
|
459
|
+
holder.itemView.setOnClickListener(new View.OnClickListener() {
|
460
|
+
|
461
|
+
@Override
|
462
|
+
|
463
|
+
public void onClick(View v) {
|
464
|
+
|
465
|
+
final String id = obj.getId();
|
466
|
+
|
467
|
+
Log.d(TAG, "id onClick itemView = " + id);
|
468
|
+
|
469
|
+
Log.d(TAG, "onClick itemView = " + memoListActivity);
|
470
|
+
|
471
|
+
memoListActivity.replaceToEditMemoFragment(id);
|
472
|
+
|
473
|
+
}
|
474
|
+
|
475
|
+
});
|
476
|
+
|
477
|
+
}
|
478
|
+
|
479
|
+
|
480
|
+
|
481
|
+
@Override
|
482
|
+
|
483
|
+
public int getItemCount() {
|
484
|
+
|
485
|
+
return mDataset.size();
|
486
|
+
|
487
|
+
}
|
488
|
+
|
489
|
+
|
490
|
+
|
491
|
+
static class MemoViewHolder extends RecyclerView.ViewHolder {
|
492
|
+
|
493
|
+
TextView memo;
|
494
|
+
|
495
|
+
TextView id;
|
496
|
+
|
497
|
+
|
498
|
+
|
499
|
+
MemoViewHolder(View view) {
|
500
|
+
|
501
|
+
super(view);
|
502
|
+
|
503
|
+
memo = view.findViewById(R.id.memoContent);
|
504
|
+
|
505
|
+
id = view.findViewById(R.id.memoId);
|
506
|
+
|
507
|
+
}
|
508
|
+
|
509
|
+
}
|
510
|
+
|
511
|
+
}
|
512
|
+
|
513
|
+
```
|
1
関連コード追記
test
CHANGED
File without changes
|
test
CHANGED
@@ -167,3 +167,223 @@
|
|
167
167
|
|
168
168
|
|
169
169
|
```
|
170
|
+
|
171
|
+
関連コード
|
172
|
+
|
173
|
+
---
|
174
|
+
|
175
|
+
・メインアクティビティ
|
176
|
+
|
177
|
+
``````ここに言語を入力
|
178
|
+
|
179
|
+
public class MemoListActivity extends AppCompatActivity {
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
@Override
|
184
|
+
|
185
|
+
protected void onCreate(Bundle savedInstanceState) {
|
186
|
+
|
187
|
+
super.onCreate(savedInstanceState);
|
188
|
+
|
189
|
+
setContentView(R.layout.activity_memolist);
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
if (findViewById(R.id.container) != null) {
|
194
|
+
|
195
|
+
if (savedInstanceState != null) {return;}
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
TaskListFragment firstFragment = new TaskListFragment();
|
200
|
+
|
201
|
+
firstFragment.setArguments(getIntent().getExtras());
|
202
|
+
|
203
|
+
getSupportFragmentManager().beginTransaction().add(R.id.container, firstFragment).commit();
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
//ボトムナビゲーション
|
210
|
+
|
211
|
+
BottomNavigationView bottomNavigationView = (BottomNavigationView)
|
212
|
+
|
213
|
+
findViewById(R.id.bottom_navigation);
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
bottomNavigationView.setOnNavigationItemSelectedListener(
|
218
|
+
|
219
|
+
new BottomNavigationView.OnNavigationItemSelectedListener() {
|
220
|
+
|
221
|
+
@Override
|
222
|
+
|
223
|
+
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
|
224
|
+
|
225
|
+
switch (item.getItemId()) {
|
226
|
+
|
227
|
+
case R.id.action_memo: {
|
228
|
+
|
229
|
+
MemoListFragment fragment = MemoListFragment.newInstance();
|
230
|
+
|
231
|
+
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
|
232
|
+
|
233
|
+
return true;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
case R.id.action_task: {
|
238
|
+
|
239
|
+
...
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
return false;
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
});
|
250
|
+
|
251
|
+
|
252
|
+
|
253
|
+
//Realm初期化・アプリ起動時のActivityに記述
|
254
|
+
|
255
|
+
Realm.init(this);
|
256
|
+
|
257
|
+
RealmConfiguration config = new RealmConfiguration.Builder().build();
|
258
|
+
|
259
|
+
Realm.setDefaultConfiguration(config);
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
```
|
266
|
+
|
267
|
+
・containerに表示されるフラグメント(onCreateView内でアダプタがnewされている)
|
268
|
+
|
269
|
+
```ここに言語を入力
|
270
|
+
|
271
|
+
public class MemoListFragment extends Fragment {
|
272
|
+
|
273
|
+
private FloatingActionButton fab;
|
274
|
+
|
275
|
+
private MemoListActivity memoListActivity;
|
276
|
+
|
277
|
+
private RecyclerView recyclerView;
|
278
|
+
|
279
|
+
private Realm realm;
|
280
|
+
|
281
|
+
private MemoAdapter adapter;
|
282
|
+
|
283
|
+
|
284
|
+
|
285
|
+
public static MemoListFragment newInstance(){
|
286
|
+
|
287
|
+
MemoListFragment f = new MemoListFragment();
|
288
|
+
|
289
|
+
Bundle args = new Bundle();
|
290
|
+
|
291
|
+
f.setArguments(args);
|
292
|
+
|
293
|
+
return f;
|
294
|
+
|
295
|
+
}
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
@Override
|
300
|
+
|
301
|
+
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
302
|
+
|
303
|
+
Bundle savedInstanceState) {
|
304
|
+
|
305
|
+
View view = inflater.inflate(R.layout.fragment_memo_list, container, false);
|
306
|
+
|
307
|
+
fab = view.findViewById(R.id.intentEditMemo);
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
realm = Realm.getDefaultInstance(); // Realmをインスタンス化
|
312
|
+
|
313
|
+
final RealmResults<Memo> result = realm.where(Memo.class).findAll() // テーブルから全レコード取得
|
314
|
+
|
315
|
+
.sort("id", Sort.DESCENDING);// 降順にソート
|
316
|
+
|
317
|
+
adapter = new MemoAdapter(result); // その結果をアダプターに渡す
|
318
|
+
|
319
|
+
|
320
|
+
|
321
|
+
recyclerView = view.findViewById(R.id.memoList);
|
322
|
+
|
323
|
+
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
|
324
|
+
|
325
|
+
recyclerView.setAdapter(adapter);
|
326
|
+
|
327
|
+
recyclerView.setHasFixedSize(true);
|
328
|
+
|
329
|
+
recyclerView.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
|
330
|
+
|
331
|
+
|
332
|
+
|
333
|
+
return view;
|
334
|
+
|
335
|
+
}
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
@Override
|
340
|
+
|
341
|
+
public void onStart() {
|
342
|
+
|
343
|
+
super.onStart();
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
//新規作成ボタン
|
348
|
+
|
349
|
+
fab.setOnClickListener(new View.OnClickListener() {
|
350
|
+
|
351
|
+
@Override
|
352
|
+
|
353
|
+
public void onClick(View view) {
|
354
|
+
|
355
|
+
Bundle bundle = new Bundle();
|
356
|
+
|
357
|
+
bundle.putString("id", "");
|
358
|
+
|
359
|
+
EditMemoFragment fragment = new EditMemoFragment();
|
360
|
+
|
361
|
+
fragment.setArguments(bundle);
|
362
|
+
|
363
|
+
FragmentTransaction transaction = getFragmentManager().beginTransaction();
|
364
|
+
|
365
|
+
transaction.replace(R.id.container, fragment.newInstance(),"EditMemoFragment").commit();
|
366
|
+
|
367
|
+
}
|
368
|
+
|
369
|
+
});
|
370
|
+
|
371
|
+
}
|
372
|
+
|
373
|
+
|
374
|
+
|
375
|
+
@Override
|
376
|
+
|
377
|
+
public void onDestroy() {
|
378
|
+
|
379
|
+
super.onDestroy();
|
380
|
+
|
381
|
+
recyclerView.setAdapter(null);
|
382
|
+
|
383
|
+
realm.close();
|
384
|
+
|
385
|
+
}
|
386
|
+
|
387
|
+
}
|
388
|
+
|
389
|
+
```
|