NotifyDataSetChanged不会更新我的ListView

时间:2014-10-09 18:28:36

标签: c# android listview xamarin

即使我正在重读序列化的xml文件,NotifyDataSetChanged()也不会更新我的ListView。它只会在再次调用oncreate后更新。阅读一些Java代码,它告诉我重新填充我的listadapter,但仍然无效。有什么想法吗?

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
            {

                View view = inflater.Inflate(Resource.Layout.Card, null);

                listView = view.FindViewById<ListView>(Resource.Id.cardListView);

                return view;
            }


            public override void OnResume()
            {
                base.OnResume();
                List<string> nameList = new List<string>();
                string documentsPath = Android.OS.Environment.ExternalStorageDirectory + "/Gate";
                var cardPath = Path.Combine(documentsPath, "card.xml");

                if (File.Exists(cardPath))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(List<Card>));
                    StreamReader reader = new StreamReader(cardPath);
                    cardList = (List<Card>)serializer.Deserialize(reader);
                    reader.Close();
                }
                else
                {
                    cardList = new List<Card>();
                }

                foreach (Card card in cardList)
                {
                    nameList.Add(card.name);
                }

                var adapter = new ArrayAdapter<string>(this.Activity, Android.Resource.Layout.SimpleListItem1, nameList);

                listView.Adapter = adapter;

                adapter.NotifyDataSetChanged();
            }

1 个答案:

答案 0 :(得分:1)

您应该只在OnCreate中创建一次适配器并存储它。

然后,在OnResume()中,仅使用adapter.AddAll(nameList)更新值并调用NotifyDataSetChanged();