Xamarin:旋转屏幕后从ArrayAdapter获取数据

时间:2015-09-28 18:52:55

标签: c# android xamarin android-arrayadapter

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

提前致谢!!

1 个答案:

答案 0 :(得分:2)

当您旋转屏幕时,您当前的活动正在被拆除。任何没有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);}