在Xamarin
添加一个TextView到ListView
我想一个简单的TextView添加到ListView,但我不知道正确的代码。
这里是我当前的代码:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
SetContentView (Resource.Layout.Main);
ListView listView = new ListView(this.ApplicationContext);
TextView textView = new TextView (this.ApplicationContext);
textView.Text = "Example text";
View view = new View (this.ApplicationContext);
}
我可以请有一定的帮助?
编辑
这里是我的代码:
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
ListView lst = (ListView) FindViewById(Resource.Id.listView);
// create the ArrayList to store the titles of nodes
List<String> listItems = new List<String>();
listItems.Add ("Test 1");
listItems.Add ("Test 2");
ArrayAdapter ad = new ArrayAdapter(this.ApplicationContext, Resource.Layout.simple_list_item_1, listItems);
// give adapter to ListView UI element to render
lst.SetAdapter(ad);
}
我收到此错误:
UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
我可以请有一定的帮助与此代码?
在此先感谢
您需要创建一个自定义适配器或者使用simpleAdapter或ArrayAdapter在ListView中显示文本。 –
你想在ListView的什么位置添加这个TextView? –
在第一个位置。 – user22707