在Xamarin Android中动态填充空listView

时间:2017-03-13 09:35:15

标签: listview xamarin.android

我有一个listView需要填充,因为我从一个函数动态获取值。当我在onCreate方法中将我的字符串添加到适配器时,我的代码工作。如何从AddDiscoveryEntry()填充我的空listView? 以下是我的代码:

ListView listView1;
List<string> itemList;
ArrayAdapter<string> adapter;
public class MainActivity : Activity
{
    public delegate void AsyncCallBack(IAsyncResult ar);

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

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

        listView1 = FindViewById<ListView>(Resource.Id.listView1);
        itemList = new List<string>(new[] { "Some item" });
        adapter = new ArrayAdapter<string>(this,Android.Resource.Layout.SimpleListItem1);//, itemList);
        listView1.Adapter = adapter;
    }

    public void ReceiveCallBack(IAsyncResult ar)
    {
        AddDiscoveryEntry(a_string_variable);
    }

    public void AddDiscoveryEntry(string o)
    {
        //itemList.Add(o);
        adapter.Add(o);
        adapter.NotifyDataSetChanged();
    }
}

0 个答案:

没有答案