【发布时间】:2023-04-01 09:34:01
【问题描述】:
你好我有以下代码
public class MainActivity : Activity
{
Button b;
Button c;
TextView t;
List<string> tasks = new List<string>();
ListView lView;
ArrayAdapter<string> adapter;
int count = 0;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
b = FindViewById<Button>(Resource.Id.btn);
c = FindViewById<Button>(Resource.Id.clearBtn);
t = FindViewById<TextView>(Resource.Id.tView);
lView = FindViewById<ListView>(Resource.Id.listView);
adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1,tasks);
lView.Adapter = adapter;
b.Click += ChangeTextAndAdd;
}
}
private void ChangeTextAndAdd(object sender, EventArgs e)
{
t.Text = "text is changed";
string listItem = string.Format("task{0}", count++);
tasks.Add(listItem);
adapter.NotifyDataSetChanged();
}
我的问题是为什么当我单击按钮时我的列表视图没有更新。我不明白,因为我使用过adapter.NotifyDataSetChanged();,但它不起作用。有什么我遗漏的吗?
【问题讨论】:
-
试试这个
listView.setAdapter(adapter); adapter.notifyDataSetChanged(); -
也不起作用。这可能是由于 Visual Studio 中的错误造成的吗?因为我之前已经厌倦了
NotifyDataSetChaged(),而且效果很好。
标签: c# xamarin.android refresh adapter