简体   繁体   English

ListView不更新(C#)

[英]ListView not updating (c#)

I am doing a simple test because I need to learn about the ListView. 我正在做一个简单的测试,因为我需要了解ListView。 I've done a lot of searching and there is a LOT out there but all of it trying to do more than just the basics. 我已经做了很多搜索,并且还有很多,但是所有这些都试图做更多而不只是基础知识。 So to understand the concept I've done a simple program and listed it below. 因此,要理解这个概念,我做了一个简单的程序,并在下面列出。

Everything works - except when i rotate the phone. 一切正常-除非我旋转手机。 I understand that it will destroy the Activity and I need to re-create the listview and adapter. 我知道它将破坏活动,因此我需要重新创建列表视图和适配器。 What I don't understand is - why my list of strings is also "destroyed". 我不明白的是-为什么我的字符串列表也被“破坏”了。

Looking at my example - the weekdays will be there after rotation. 看我的例子-工作日将在轮班之后出现。 But the list of click-times will not !! 但是点击时间列表不会! What would be the right way to use the listview in my case? 在我的情况下,使用listview的正确方法是什么? I'm not trying to do complicated row views or custom adapters. 我不尝试做复杂的行视图或自定义适配器。 I'll go with the basics. 我将介绍基础知识。 Thanks ! 谢谢 !

namespace NewListViewTest
{
    [Activity(Label = "NewListViewTest", MainLauncher = true)]
    public class MainActivity : Activity
    {
        Button StartScan;
        Button StopScan;
        TextView Filter;
        ListView IBeaconActivities;
        List<string> dataList;
        ArrayAdapter<string> dataAdapter;

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.Main);

            dataList = new List<string>();
        }

        protected override void OnStart()
        {
            base.OnStart();
            StartScan = FindViewById<Button>(Resource.Id.startScan);
            StartScan.Click += StartScan_Click;

            StopScan = FindViewById<Button>(Resource.Id.stopScan);
            Filter = FindViewById<TextView>(Resource.Id.filter);
            IBeaconActivities = FindViewById<ListView>(Resource.Id.beaconActivities);

            dataAdapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, dataList);
            IBeaconActivities.Adapter = dataAdapter;

            dataList.Add("Monday");
            dataList.Add("Tuesday");
            dataList.Add("Wednesday");
            dataList.Add("Thursday");
            dataList.Add("Friday");
            dataList.Add("Saturday");
            dataList.Add("Sunday");

            DataWasUpdated();
        }

        public void DataWasUpdated()
        {
            dataAdapter.Clear();
            dataAdapter.AddAll(dataList);

            dataAdapter.NotifyDataSetChanged();
        }

        private void StartScan_Click(object sender, System.EventArgs e)
        {
            dataList.Add("New item at: " + DateTime.Now.ToString("hh:mm:ss"));
            DataWasUpdated();
        }
    }
}

ListView not updating (c#) ListView不更新(C#)

You could read the Android Activity Lifecycle document, when you rotate the phone, your Activity will be destroyed and recreate. 您可以阅读Android Activity Lifecycle文档,旋转手机时, Activity将被销毁并重新创建。 You could add a log to see more information, for example : 您可以添加日志以查看更多信息,例如:

protected override void OnPause()
{
    base.OnPause();
    Log.Error(Tag, "OnPause");
}

protected override void OnResume()
{
    base.OnResume();
    Log.Error(Tag, "OnResume");
}

protected override void OnSaveInstanceState(Bundle outState)
{
    base.OnSaveInstanceState(outState);
    Log.Error(Tag, "OnSaveInstanceState");
}

protected override void OnRestoreInstanceState(Bundle savedInstanceState)
{
    base.OnRestoreInstanceState(savedInstanceState);
    Log.Error(Tag, "OnRestoreInstanceState");
}

protected override void OnDestroy()
{
    base.OnDestroy();
    Log.Error(Tag, "OnDestroy");
}

......

When you rotate your phone, you could see following information : 旋转手机时,您会看到以下信息:

在此处输入图片说明

That's why your ListView will be destroyed. 这就是为什么您的ListView将被销毁的原因。

Solution : 解决方案:

Add a ConfigurationChanges attribute in your Activity : 在您的Activity添加ConfigurationChanges属性:

[Activity(Label = "ListActivity", 
    ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
public class ListActivity : Activity

Then, when you rotate your phone only execute the OnConfigurationChanged method : 然后,在旋转手机时,仅执行OnConfigurationChanged方法:

E/Activity (17410): OnConfigurationChanged

Your Activity won't be created and your New item will still exist. 您的Activity将不会被创建,并且您的New item仍将存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM