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