gpt4 book ai didi

c# - NotifyDataSetChanged 不更新我的 ListView

转载 作者:行者123 更新时间:2023-11-30 02:37:23 25 4
gpt4 key购买 nike

NotifyDataSetChanged() 不会更新我的 ListView,即使我正在重新读取序列化的 xml 文件。它仅在再次调用 oncreate 后更新。阅读一些 Java 代码,它告诉我重新填充我的列表适配器,但这仍然不起作用。有什么想法吗?

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();
}

最佳答案

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

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

关于c# - NotifyDataSetChanged 不更新我的 ListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26285536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com