【发布时间】:2016-04-27 17:21:34
【问题描述】:
我只想用数据填充 Xamarin 中的 ListView 并更新它,以便项目可见。现在我搜索了很多并尝试了这个:
List<string> items = new List<string> ();
ArrayAdapter ListAdapter = null;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
Button button = FindViewById<Button>(Resource.Id.MyButton);
ListView listView = FindViewById<ListView>(Resource.Id.listView1);
ListAdapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleListItem1, items);
listView.Adapter = ListAdapter;
button.Click += delegate { Server(listView); };
}
现在“服务器”方法有以下代码:
items.Add("The server is running at port localEndPoint...");
ListAdapter.NotifyDataSetChanged();
这绝对没有任何作用。什么都没有更新。我也试过:
RunOnUiThread(() =>
{
ListAdapter.NotifyDataSetChanged();
});
但在这里它甚至没有跳转到 RunOnUiThread。
我做错了什么?我对 Xamarin 很陌生。
更新:完整的服务器代码:
try
{
IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
/* Initializes the Listener */
TcpListener myList = new TcpListener(ipAddress, 8001);
/* Start Listeneting at the specified port */
myList.Start();
ListAdapter.Add("The server is running at port localEndPoint...");
RunOnUiThread(() =>
{
ListAdapter.NotifyDataSetChanged();
});
Console.WriteLine("The local End point is :" +
myList.LocalEndpoint);
Console.WriteLine("Waiting for a connection.....");
Socket s = myList.AcceptSocket();
Console.WriteLine("Connection accepted from " + s.RemoteEndPoint);
byte[] b = new byte[100];
int k = s.Receive(b);
Console.WriteLine("Recieved...");
for (int i = 0; i < k; i++)
Console.Write(Convert.ToChar(b[i]));
ASCIIEncoding asen = new ASCIIEncoding();
s.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
/* clean up */
s.Close();
myList.Stop();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
【问题讨论】:
-
我想你的 listView 是空的。验证您 FindViewById
(Resource.Id.listView1);确实返回一个非空值。
标签: c# listview xamarin populate