Xamarinでのアプリ開発でExpandableListViewを使用しているのですが、アプリ内で追加した親リストが画面遷移で消えてしまいます。
OnSaveInstanceState
を使えばいいのかなと思うのですが、使い方がいまいちわかりません。またほかに原因・対策が分かれば教えていただきたいです。
using Android.App;
using Android.Widget;
using Android.OS;
using System.Collections.Generic;
using Android.Content;
using PCLStorage;
using Android.Runtime;
using Temo1.Resources.layout;
using Android.Views;
using static Android.Resource;
namespace Temo1
{
[Activity(Label = "Temo1", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
Button memobutton = FindViewById<Button>(Resource.Id.button1);
Button notebutton = FindViewById<Button>(Resource.Id.notebutton);
ExpandableListView memolist = FindViewById<ExpandableListView>(Resource.Id.expandableListView1);
string[] filename = new string[100];
int n;
var localFolder = FileSystem.Current.LocalStorage;
var files = localFolder.GetFilesAsync().Result;
//親リストgroupList子リストchildList
JavaList<IDictionary<string, object>> groupList = new JavaList<IDictionary<string, object>>();
JavaList<IList<IDictionary<string, object>>> childList = new JavaList<IList<IDictionary<string, object>>>();
//デフォルトのグループ"全て"
JavaDictionary<string, object> groupElement = new JavaDictionary<string, object>();
groupElement.Add("GROUP_TITLE", "ALL");
groupList.Add(groupElement);
//子リスト用の文字列(ファイル名)を配列に用意
n = 0;
foreach (var file in files)
{
var fn = file.Name;
fn = fn.Remove(fn.Length - 4);//拡張子を表示しないように末尾を削除
filename[n] = fn;//配列にファイル名を格納
n++;
}
JavaList<IDictionary<string, object>> ChildElements = new JavaList<IDictionary<string, object>>();
for (int j = 0; j <= n - 1; j++)
{
JavaDictionary<string, object> child = new JavaDictionary<string, object>();
child.Add("CHILD_TITLE", filename[j]);
ChildElements.Add(child);
}
childList.Add(ChildElements);
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
groupList,
Android.Resource.Layout.SimpleExpandableListItem1,
new string[] { "GROUP_TITLE" },
new int[] { Android.Resource.Id.Text1 },
childList,
Android.Resource.Layout.SimpleExpandableListItem2,
new string[] { "CHILD_TITLE" },
new int[] { Android.Resource.Id.Text2 }
);
memolist.SetAdapter(adapter);
//親リスト追加
//リスト名入力ダイアログ
notebutton.Click += (_, __) =>
{
var layout = LayoutInflater.Inflate(Resource.Layout.dlg1, null);
var dlg = new AlertDialog.Builder(this);
dlg.SetTitle("ノート名");
dlg.SetView(layout); //<=axmlで指定したビューを指定
dlg.SetPositiveButton("OK", (s, a) => {
//axml内のビューへのポインタの取得
var notename = layout.FindViewById<EditText>(Resource.Id.dlgtxt);
Toast.MakeText(this, notename.Text, ToastLength.Short).Show();
if (!string.IsNullOrEmpty(notename.Text))
{
var newGroupElement = new JavaDictionary<string, object>();
newGroupElement.Add("GROUP_TITLE", notename.Text);
groupList.Add(newGroupElement);
childList.Add(new JavaList<IDictionary<string, object>>());
adapter.NotifyDataSetChanged();
}
else
{
// 未入力エラー
Toast.MakeText(this, "未入力です", ToastLength.Short).Show();
}
});
dlg.Create().Show();
};
//メモ作成画面へ遷移
memobutton.Click += (_, __) =>
{
var intent = new Intent(this, typeof(memocreate));
StartActivity(intent);
};
//リストアイテムがクリックされたらテキスト編集に遷移
memolist.ChildClick+= async (sender, e) =>
{
var parent = (ExpandableListView)e.Parent;
var packedPosition = parent.GetExpandableListPosition(e.ChildPosition);
var groupPosition = ExpandableListView.GetPackedPositionGroup(packedPosition);
var childPosition = ExpandableListView.GetPackedPositionChild(packedPosition);
var type = ExpandableListView.GetPackedPositionType(packedPosition);
if (type == PackedPositionType.Child)
{
var item = (IDictionary<string, object>)parent.ExpandableListAdapter.GetChild(groupPosition, childPosition+1);
var clickfile = item["CHILD_TITLE"].ToString();
//そのファイルのテキストを取得
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFile file = await rootFolder.GetFileAsync(clickfile + ".txt");//ファイルを読むために拡張子をつける
string saveddata = await file.ReadAllTextAsync();
var intent = new Intent(this, typeof(memoedit));
intent.PutExtra("clickfile", clickfile); // intentに値を入れる
intent.PutExtra("Data", saveddata);
StartActivity(intent);
}
};
//長押しされたアイテムを削除
memolist.ItemLongClick += async (sender, e) =>
{
var parent = (ExpandableListView)e.Parent;
var packedPosition = parent.GetExpandableListPosition(e.Position);
var groupPosition = ExpandableListView.GetPackedPositionGroup(packedPosition);
var childPosition = ExpandableListView.GetPackedPositionChild(packedPosition);
var type = ExpandableListView.GetPackedPositionType(packedPosition);
if (type == PackedPositionType.Child)
{
var item = (IDictionary<string, object>)parent.ExpandableListAdapter.GetChild(groupPosition, childPosition);
var deletefile = item["CHILD_TITLE"].ToString();
//adapterから削除し更新
childList[groupPosition].Remove(item);
adapter.NotifyDataSetChanged();
//元ファイル削除
IFolder rootFolder = FileSystem.Current.LocalStorage;
IFile file = await rootFolder.GetFileAsync(deletefile + ".txt");
await file.DeleteAsync();
}
};
//検索
EditText searchtext = FindViewById<EditText>(Resource.Id.searchtext);
searchtext.Click += (sender, e) =>
{
searchtext.Text = "";
};
}
}
}