Xamarin:旋转屏幕后从ArrayAdapter获取数据
Xamarin : Get data from ArrayAdapter after rotating screen
旋转屏幕时,ArrayAdapter 中的数据会消失。有人知道如何解决吗?
我的代码在MainActivity.cs
private int count = 0;
private IList<string> intervals = new List<string>();
private ArrayAdapter<string> listAdapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
this.timeView = FindViewById<TextView>(Resource.Id.timeView);
this.interval = FindViewById<ListView>(Resource.Id.interval);
this.listAdapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1, intervals);
this.interval.Adapter = listAdapter;
RunOnUiThread(() => { listAdapter.NotifyDataSetChanged();});
this.timeView.Click += (IntentSender, eventArgs) => addInterval();
}
private void addInterval() {
this.listAdapter.Insert(++this.count + "\t\t\t\t" + string.Format("{0:hh\:mm\:ss\.fff}",this.accumulatedTime),0);
}
提前致谢!!
旋转屏幕时,当前的 activity 正在被拆除。任何没有 id
的项目都将失去它的价值。
因此尝试使用
public void onSaveInstanceState(Bundle savedState) {
super.onSaveInstanceState(savedState);
String[] aavalues = aAdapter.getValue();
savedState.putStringArray("enter a key value here", aavalues);}
您必须定义一个 getValue
方法来获取适配器中的值。
然后因为你的 activity 被摧毁它会再次进入 oncreate
。
在那里初始化你的适配器
String[] avalues = savedInstanceState.getStringArray("the key value from before");
if (values != null) {
aAdaptor = new MyAdaptor(avalues);}
旋转屏幕时,ArrayAdapter 中的数据会消失。有人知道如何解决吗?
我的代码在MainActivity.cs
private int count = 0;
private IList<string> intervals = new List<string>();
private ArrayAdapter<string> listAdapter;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
this.timeView = FindViewById<TextView>(Resource.Id.timeView);
this.interval = FindViewById<ListView>(Resource.Id.interval);
this.listAdapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1, intervals);
this.interval.Adapter = listAdapter;
RunOnUiThread(() => { listAdapter.NotifyDataSetChanged();});
this.timeView.Click += (IntentSender, eventArgs) => addInterval();
}
private void addInterval() {
this.listAdapter.Insert(++this.count + "\t\t\t\t" + string.Format("{0:hh\:mm\:ss\.fff}",this.accumulatedTime),0);
}
提前致谢!!
旋转屏幕时,当前的 activity 正在被拆除。任何没有 id
的项目都将失去它的价值。
因此尝试使用
public void onSaveInstanceState(Bundle savedState) {
super.onSaveInstanceState(savedState);
String[] aavalues = aAdapter.getValue();
savedState.putStringArray("enter a key value here", aavalues);}
您必须定义一个 getValue
方法来获取适配器中的值。
然后因为你的 activity 被摧毁它会再次进入 oncreate
。
在那里初始化你的适配器
String[] avalues = savedInstanceState.getStringArray("the key value from before");
if (values != null) {
aAdaptor = new MyAdaptor(avalues);}