旋转屏幕时,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);
        }


提前致谢!!

最佳答案

旋转屏幕时,当前活动被拆除。没有id的任何项目都将失去其价值。

因此尝试使用

public void onSaveInstanceState(Bundle savedState) {
super.onSaveInstanceState(savedState);
String[] aavalues = aAdapter.getValue();
savedState.putStringArray("enter a key value here", aavalues);}


您必须定义一个getValue方法来获取适配器内部的值。

然后,由于您的活动被销毁,它将再次输入oncreate

在此处初始化适配器

    String[] avalues = savedInstanceState.getStringArray("the key value from before");
    if (values != null) {
       aAdaptor = new MyAdaptor(avalues);}

08-26 00:45