Xamarin.Android从ListView单击事件获取对象属性 [英] Xamarin.Android get object properties from ListView click event
问题描述
我对C#(几个星期)和Xamarin(大约一个星期)很陌生.
我能够通过教程在Android上的ListView中显示实体集合(//从 我的事件目标代码是: 然后该活动中的代码为 然后 我不确定listView_ItemClick是解决方法还是其他方法.任何建议将不胜感激 您订阅的事件具有一些不错的参数.如果您已经探究过 所以基本上您会遇到类似这样的事件: I am very new to C# (a few weeks) and Xamarin (about a week). I was able to implement the ListView Adapter from the tutorial "Display Entity Collection in ListView on Android (//from http://diptimayapatra.wordpress.com/2013/07/08/xamarin-display-entity-collection-in-listview-in-android/) My problem now is that I have no idea how to handle the click event on the TextView text. The GetView code from my Adapter is: My Incident object code is: then the code in the activity is then I am not sure if the listView_ItemClick is the way to go or is there some other way. Any suggestions will be greatly appreciated The event you subscribe to has some nice arguments. If you had explored what you get in the So basically you can get an incident like:
这篇关于Xamarin.Android从ListView单击事件获取对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!public class Incident
{
public int id {get; set;}
public string title {get; set;}
public string description {get; set;}
public double latitude {get; set;}
public double longitude {get; set;}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
listView = FindViewById<ListView>(Resource.Id.list);
IncidentGet incGet = new IncidentGet();
List<Incident> incidents = incGet.GetIncidentData()
listAdapter = new ListViewAdapter(this, incidents);
listView.Adapter = listAdapter;
listView.ItemClick += listView_ItemClick;
}
void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//have no idea how to get the properties of each Incident object here
}
AdapterView.ItemClickEventArgs
中的内容,那么它将表明存在Position
属性,该属性基本上为您提供了一种从Adapter
中获取项目的方法,这是单击的View
所代表的.>
void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var incident = incidents[e.Position];
// do whatever with that incident here...
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
var incident = incidents[position];
View view = convertView;
if(view == null)
{
view = context.LayoutInflater.Inflate(
Resource.Layout.ListViewTemplate, null);
}
view.FindViewById<TextView>(Resource.Id.tvIncident).Text =
string.Format("{0}", incident.title);
view.FindViewById<TextView>(Resource.Id.tvIncidentDescription).Text =
string.Format("{0}", incident.description);
return view;
}
public class Incident
{
public int id {get; set;}
public string title {get; set;}
public string description {get; set;}
public double latitude {get; set;}
public double longitude {get; set;}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
listView = FindViewById<ListView>(Resource.Id.list);
IncidentGet incGet = new IncidentGet();
List<Incident> incidents = incGet.GetIncidentData()
listAdapter = new ListViewAdapter(this, incidents);
listView.Adapter = listAdapter;
listView.ItemClick += listView_ItemClick;
}
void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//have no idea how to get the properties of each Incident object here
}
AdapterView.ItemClickEventArgs
it would reveal that there is a Position
property, which basically gets you a way to get the item from your Adapter
, which the clicked View
represents.void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var incident = incidents[e.Position];
// do whatever with that incident here...
}