質問編集履歴

1

情報不足だったため加筆。

2018/10/29 03:06

投稿

sezaki_H
sezaki_H

スコア41

test CHANGED
File without changes
test CHANGED
@@ -49,3 +49,441 @@
49
49
  };
50
50
 
51
51
  ```
52
+
53
+
54
+
55
+ 全体コード
56
+
57
+ ```c#
58
+
59
+ using Android.App;
60
+
61
+ using Android.Widget;
62
+
63
+ using Android.OS;
64
+
65
+ using System.Collections.Generic;
66
+
67
+ using Android.Content;
68
+
69
+ using PCLStorage;
70
+
71
+ using Android.Runtime;
72
+
73
+ using Temo1.Resources.layout;
74
+
75
+ using Android.Views;
76
+
77
+ using static Android.Resource;
78
+
79
+
80
+
81
+ namespace Temo1
82
+
83
+ {
84
+
85
+ [Activity(Label = "Temo1", MainLauncher = true)]
86
+
87
+ public class MainActivity : Activity
88
+
89
+ {
90
+
91
+ protected override void OnCreate(Bundle savedInstanceState)
92
+
93
+ {
94
+
95
+ base.OnCreate(savedInstanceState);
96
+
97
+
98
+
99
+ // Set our view from the "main" layout resource
100
+
101
+ SetContentView(Resource.Layout.Main);
102
+
103
+
104
+
105
+
106
+
107
+
108
+
109
+ Button memobutton = FindViewById<Button>(Resource.Id.button1);
110
+
111
+ Button notebutton = FindViewById<Button>(Resource.Id.notebutton);
112
+
113
+ ExpandableListView memolist = FindViewById<ExpandableListView>(Resource.Id.expandableListView1);
114
+
115
+
116
+
117
+ string[] filename = new string[100];
118
+
119
+ int n;
120
+
121
+ var localFolder = FileSystem.Current.LocalStorage;
122
+
123
+ var files = localFolder.GetFilesAsync().Result;
124
+
125
+
126
+
127
+
128
+
129
+ //親リストgroupList子リストchildList
130
+
131
+ List<IDictionary<string, object>> groupList = new List<IDictionary<string, object>>();
132
+
133
+ List<IList<IDictionary<string, object>>> childList = new List<IList<IDictionary<string, object>>>();
134
+
135
+
136
+
137
+ //デフォルトのグループ"全て"
138
+
139
+ JavaDictionary<string, object> groupElement = new JavaDictionary<string, object>();
140
+
141
+ groupElement.Add("GROUP_TITLE", "ALL");
142
+
143
+ groupList.Add(groupElement);
144
+
145
+
146
+
147
+
148
+
149
+
150
+
151
+
152
+
153
+
154
+
155
+ //子リスト用の文字列(ファイル名)を配列に用意
156
+
157
+ n = 0;
158
+
159
+ foreach (var file in files)
160
+
161
+ {
162
+
163
+ var fn = file.Name;
164
+
165
+ fn = fn.Remove(fn.Length - 4);//拡張子を表示しないように末尾を削除
166
+
167
+ filename[n] = fn;//配列にファイル名を格納
168
+
169
+ n++;
170
+
171
+ }
172
+
173
+
174
+
175
+
176
+
177
+ JavaList<IDictionary<string, object>> childElements = new JavaList<IDictionary<string, object>>();
178
+
179
+ for (int j = 0; j <= n - 1; j++)
180
+
181
+ {
182
+
183
+ JavaDictionary<string, object> child = new JavaDictionary<string, object>();
184
+
185
+ child.Add("CHILD_TITLE", filename[j]);
186
+
187
+ childElements.Add(child);
188
+
189
+ }
190
+
191
+ childList.Add(childElements);
192
+
193
+
194
+
195
+ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
196
+
197
+ this,
198
+
199
+ groupList,
200
+
201
+ Android.Resource.Layout.SimpleExpandableListItem1,
202
+
203
+ new string[] { "GROUPE_TITLE" },
204
+
205
+ new int[] { Android.Resource.Id.Text1 },
206
+
207
+ childList,
208
+
209
+ Android.Resource.Layout.SimpleExpandableListItem2,
210
+
211
+ new string[] { "CHILD_TITLE" },
212
+
213
+ new int[] { Android.Resource.Id.Text2 }
214
+
215
+ );
216
+
217
+ memolist.SetAdapter(adapter);
218
+
219
+
220
+
221
+
222
+
223
+ //親リスト追加
224
+
225
+
226
+
227
+ //リスト名入力ダイアログ
228
+
229
+ notebutton.Click += (_, __) =>
230
+
231
+ {
232
+
233
+ var layout = new LinearLayout(this) { Orientation = Orientation.Vertical };
234
+
235
+ layout.SetGravity(GravityFlags.Left);
236
+
237
+ var notename = new EditText(this);
238
+
239
+ notename.SetMaxLines(1);
240
+
241
+ layout.AddView(new TextView(this) { Text = "ノート名" });
242
+
243
+ layout.AddView(notename);
244
+
245
+
246
+
247
+ var dlg = new AlertDialog.Builder(this);
248
+
249
+ dlg.SetTitle("ノートの名前を入力してください。");
250
+
251
+ dlg.SetView(layout); //<=作成したビューを指定
252
+
253
+
254
+
255
+ dlg.SetPositiveButton("OK", (s, a) =>
256
+
257
+ {
258
+
259
+ Toast.MakeText(this, notename.Text, ToastLength.Short).Show();
260
+
261
+
262
+
263
+ if (!string.IsNullOrEmpty(notename.Text))
264
+
265
+ {
266
+
267
+ var newGroupElement = new JavaDictionary<string, object>();
268
+
269
+ newGroupElement.Add("GROUPE_LIST", notename.Text);
270
+
271
+ groupList.Add(newGroupElement);
272
+
273
+ adapter.NotifyDataSetChanged();
274
+
275
+ }
276
+
277
+ else
278
+
279
+ {
280
+
281
+ // 未入力エラー
282
+
283
+ }
284
+
285
+ });
286
+
287
+ dlg.Create().Show();
288
+
289
+
290
+
291
+
292
+
293
+ };
294
+
295
+
296
+
297
+
298
+
299
+ //メモ作成画面へ遷移
300
+
301
+ memobutton.Click += (_, __) =>
302
+
303
+
304
+
305
+ {
306
+
307
+
308
+
309
+ var intent = new Intent(this, typeof(memocreate));
310
+
311
+
312
+
313
+ StartActivity(intent);
314
+
315
+
316
+
317
+
318
+
319
+ };
320
+
321
+
322
+
323
+
324
+
325
+ //リストアイテムがクリックされたらテキスト編集に遷移
326
+
327
+ memolist.ChildClick+= async (sender, e) =>
328
+
329
+ {
330
+
331
+ var parent = (ExpandableListView)e.Parent;
332
+
333
+ var packedPosition = parent.GetExpandableListPosition(e.ChildPosition);
334
+
335
+
336
+
337
+ var groupPosition = ExpandableListView.GetPackedPositionGroup(packedPosition);
338
+
339
+ var childPosition = ExpandableListView.GetPackedPositionChild(packedPosition);
340
+
341
+ var type = ExpandableListView.GetPackedPositionType(packedPosition);
342
+
343
+ if (type == PackedPositionType.Child)
344
+
345
+ {
346
+
347
+ var item = (IDictionary<string, object>)parent.ExpandableListAdapter.GetChild(groupPosition, childPosition+1);
348
+
349
+ var clickfile = item["CHILD_TITLE"].ToString();
350
+
351
+
352
+
353
+ //そのファイルのテキストを取得
354
+
355
+ IFolder rootFolder = FileSystem.Current.LocalStorage;
356
+
357
+ IFile file = await rootFolder.GetFileAsync(clickfile + ".txt");
358
+
359
+ string saveddata = await file.ReadAllTextAsync();
360
+
361
+
362
+
363
+ var intent = new Intent(this, typeof(memoedit));
364
+
365
+ intent.PutExtra("clickfile", clickfile);
366
+
367
+ intent.PutExtra("Data", saveddata);
368
+
369
+
370
+
371
+
372
+
373
+ StartActivity(intent);
374
+
375
+
376
+
377
+ }
378
+
379
+ };
380
+
381
+
382
+
383
+
384
+
385
+
386
+
387
+ //長押しされたアイテムを削除
388
+
389
+ memolist.ItemLongClick += async (sender, e) =>
390
+
391
+ {
392
+
393
+ var parent = (ExpandableListView)e.Parent;
394
+
395
+ var packedPosition = parent.GetExpandableListPosition(e.Position);
396
+
397
+
398
+
399
+ var groupPosition = ExpandableListView.GetPackedPositionGroup(packedPosition);
400
+
401
+ var childPosition = ExpandableListView.GetPackedPositionChild(packedPosition);
402
+
403
+ var type = ExpandableListView.GetPackedPositionType(packedPosition);
404
+
405
+ if (type == PackedPositionType.Child)
406
+
407
+ {
408
+
409
+ var item = (IDictionary<string, object>)parent.ExpandableListAdapter.GetChild(groupPosition, childPosition);
410
+
411
+ var deletefile = item["CHILD_TITLE"].ToString();
412
+
413
+
414
+
415
+
416
+
417
+ //adapterから削除し更新
418
+
419
+ childList[groupPosition].Remove(item);
420
+
421
+ adapter.NotifyDataSetChanged();
422
+
423
+
424
+
425
+ //元ファイル削除
426
+
427
+ IFolder rootFolder = FileSystem.Current.LocalStorage;
428
+
429
+ IFile file = await rootFolder.GetFileAsync(deletefile + ".txt");
430
+
431
+
432
+
433
+ await file.DeleteAsync();
434
+
435
+ }
436
+
437
+
438
+
439
+
440
+
441
+ };
442
+
443
+
444
+
445
+
446
+
447
+
448
+
449
+
450
+
451
+
452
+
453
+
454
+
455
+
456
+
457
+
458
+
459
+
460
+
461
+
462
+
463
+
464
+
465
+
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+
474
+
475
+
476
+
477
+
478
+
479
+
480
+
481
+
482
+
483
+ }
484
+
485
+ }
486
+
487
+ }
488
+
489
+ ```