teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

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

2018/10/29 03:06

投稿

sezaki_H
sezaki_H

スコア41

title CHANGED
File without changes
body CHANGED
@@ -23,4 +23,223 @@
23
23
  adapter.NotifyDataSetChanged();
24
24
 
25
25
  };
26
+ ```
27
+
28
+ 全体コード
29
+ ```c#
30
+ using Android.App;
31
+ using Android.Widget;
32
+ using Android.OS;
33
+ using System.Collections.Generic;
34
+ using Android.Content;
35
+ using PCLStorage;
36
+ using Android.Runtime;
37
+ using Temo1.Resources.layout;
38
+ using Android.Views;
39
+ using static Android.Resource;
40
+
41
+ namespace Temo1
42
+ {
43
+ [Activity(Label = "Temo1", MainLauncher = true)]
44
+ public class MainActivity : Activity
45
+ {
46
+ protected override void OnCreate(Bundle savedInstanceState)
47
+ {
48
+ base.OnCreate(savedInstanceState);
49
+
50
+ // Set our view from the "main" layout resource
51
+ SetContentView(Resource.Layout.Main);
52
+
53
+
54
+
55
+ Button memobutton = FindViewById<Button>(Resource.Id.button1);
56
+ Button notebutton = FindViewById<Button>(Resource.Id.notebutton);
57
+ ExpandableListView memolist = FindViewById<ExpandableListView>(Resource.Id.expandableListView1);
58
+
59
+ string[] filename = new string[100];
60
+ int n;
61
+ var localFolder = FileSystem.Current.LocalStorage;
62
+ var files = localFolder.GetFilesAsync().Result;
63
+
64
+
65
+ //親リストgroupList子リストchildList
66
+ List<IDictionary<string, object>> groupList = new List<IDictionary<string, object>>();
67
+ List<IList<IDictionary<string, object>>> childList = new List<IList<IDictionary<string, object>>>();
68
+
69
+ //デフォルトのグループ"全て"
70
+ JavaDictionary<string, object> groupElement = new JavaDictionary<string, object>();
71
+ groupElement.Add("GROUP_TITLE", "ALL");
72
+ groupList.Add(groupElement);
73
+
74
+
75
+
76
+
77
+
78
+ //子リスト用の文字列(ファイル名)を配列に用意
79
+ n = 0;
80
+ foreach (var file in files)
81
+ {
82
+ var fn = file.Name;
83
+ fn = fn.Remove(fn.Length - 4);//拡張子を表示しないように末尾を削除
84
+ filename[n] = fn;//配列にファイル名を格納
85
+ n++;
86
+ }
87
+
88
+
89
+ JavaList<IDictionary<string, object>> childElements = new JavaList<IDictionary<string, object>>();
90
+ for (int j = 0; j <= n - 1; j++)
91
+ {
92
+ JavaDictionary<string, object> child = new JavaDictionary<string, object>();
93
+ child.Add("CHILD_TITLE", filename[j]);
94
+ childElements.Add(child);
95
+ }
96
+ childList.Add(childElements);
97
+
98
+ SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
99
+ this,
100
+ groupList,
101
+ Android.Resource.Layout.SimpleExpandableListItem1,
102
+ new string[] { "GROUPE_TITLE" },
103
+ new int[] { Android.Resource.Id.Text1 },
104
+ childList,
105
+ Android.Resource.Layout.SimpleExpandableListItem2,
106
+ new string[] { "CHILD_TITLE" },
107
+ new int[] { Android.Resource.Id.Text2 }
108
+ );
109
+ memolist.SetAdapter(adapter);
110
+
111
+
112
+ //親リスト追加
113
+
114
+ //リスト名入力ダイアログ
115
+ notebutton.Click += (_, __) =>
116
+ {
117
+ var layout = new LinearLayout(this) { Orientation = Orientation.Vertical };
118
+ layout.SetGravity(GravityFlags.Left);
119
+ var notename = new EditText(this);
120
+ notename.SetMaxLines(1);
121
+ layout.AddView(new TextView(this) { Text = "ノート名" });
122
+ layout.AddView(notename);
123
+
124
+ var dlg = new AlertDialog.Builder(this);
125
+ dlg.SetTitle("ノートの名前を入力してください。");
126
+ dlg.SetView(layout); //<=作成したビューを指定
127
+
128
+ dlg.SetPositiveButton("OK", (s, a) =>
129
+ {
130
+ Toast.MakeText(this, notename.Text, ToastLength.Short).Show();
131
+
132
+ if (!string.IsNullOrEmpty(notename.Text))
133
+ {
134
+ var newGroupElement = new JavaDictionary<string, object>();
135
+ newGroupElement.Add("GROUPE_LIST", notename.Text);
136
+ groupList.Add(newGroupElement);
137
+ adapter.NotifyDataSetChanged();
138
+ }
139
+ else
140
+ {
141
+ // 未入力エラー
142
+ }
143
+ });
144
+ dlg.Create().Show();
145
+
146
+
147
+ };
148
+
149
+
150
+ //メモ作成画面へ遷移
151
+ memobutton.Click += (_, __) =>
152
+
153
+ {
154
+
155
+ var intent = new Intent(this, typeof(memocreate));
156
+
157
+ StartActivity(intent);
158
+
159
+
160
+ };
161
+
162
+
163
+ //リストアイテムがクリックされたらテキスト編集に遷移
164
+ memolist.ChildClick+= async (sender, e) =>
165
+ {
166
+ var parent = (ExpandableListView)e.Parent;
167
+ var packedPosition = parent.GetExpandableListPosition(e.ChildPosition);
168
+
169
+ var groupPosition = ExpandableListView.GetPackedPositionGroup(packedPosition);
170
+ var childPosition = ExpandableListView.GetPackedPositionChild(packedPosition);
171
+ var type = ExpandableListView.GetPackedPositionType(packedPosition);
172
+ if (type == PackedPositionType.Child)
173
+ {
174
+ var item = (IDictionary<string, object>)parent.ExpandableListAdapter.GetChild(groupPosition, childPosition+1);
175
+ var clickfile = item["CHILD_TITLE"].ToString();
176
+
177
+ //そのファイルのテキストを取得
178
+ IFolder rootFolder = FileSystem.Current.LocalStorage;
179
+ IFile file = await rootFolder.GetFileAsync(clickfile + ".txt");
180
+ string saveddata = await file.ReadAllTextAsync();
181
+
182
+ var intent = new Intent(this, typeof(memoedit));
183
+ intent.PutExtra("clickfile", clickfile);
184
+ intent.PutExtra("Data", saveddata);
185
+
186
+
187
+ StartActivity(intent);
188
+
189
+ }
190
+ };
191
+
192
+
193
+
194
+ //長押しされたアイテムを削除
195
+ memolist.ItemLongClick += async (sender, e) =>
196
+ {
197
+ var parent = (ExpandableListView)e.Parent;
198
+ var packedPosition = parent.GetExpandableListPosition(e.Position);
199
+
200
+ var groupPosition = ExpandableListView.GetPackedPositionGroup(packedPosition);
201
+ var childPosition = ExpandableListView.GetPackedPositionChild(packedPosition);
202
+ var type = ExpandableListView.GetPackedPositionType(packedPosition);
203
+ if (type == PackedPositionType.Child)
204
+ {
205
+ var item = (IDictionary<string, object>)parent.ExpandableListAdapter.GetChild(groupPosition, childPosition);
206
+ var deletefile = item["CHILD_TITLE"].ToString();
207
+
208
+
209
+ //adapterから削除し更新
210
+ childList[groupPosition].Remove(item);
211
+ adapter.NotifyDataSetChanged();
212
+
213
+ //元ファイル削除
214
+ IFolder rootFolder = FileSystem.Current.LocalStorage;
215
+ IFile file = await rootFolder.GetFileAsync(deletefile + ".txt");
216
+
217
+ await file.DeleteAsync();
218
+ }
219
+
220
+
221
+ };
222
+
223
+
224
+
225
+
226
+
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+
242
+ }
243
+ }
244
+ }
26
245
  ```