Xamarin.Android get object properties from ListView ClickView event
I am very new to C # (a few weeks) and Xamarin (about a week).
I managed to implement the ListView adapter from the tutorial "Display collection of objects in ListView on Android (// from http://diptimayapatra.wordpress.com/2013/07/08/xamarin-display-entity-collection-in-listview-in-android / )
Now my problem is that I have no idea how to handle the click event on the text of the TextView.
GetView code from my adapter:
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;
}
My Incident Object Code:
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;}
}
then the code in activity
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;
}
then
void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
//have no idea how to get the properties of each Incident object here
}
I'm not sure if listView_ItemClick is the way to go or if there is some other way. Any suggestions would be highly appreciated
source to share
The event you signed up has some good arguments. If you've looked into what you got in AdapterView.ItemClickEventArgs
, this will show that there is a property Position
that basically gives you the ability to get the item from yours Adapter
that represents the clicked one View
.
So, basically you can get an incident like:
void listView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
var incident = incidents[e.Position];
// do whatever with that incident here...
}
source to share
how to pass data to each other by list on click element in xamarin
protected override void OnCreate (Bundle bundle)
listView.ItemClick+= delegate(object sender, AdapterView.ItemClickEventArgs position)
{
String selectedFromList =(String) (listView.GetItemAtPosition(position.Position));
Intent i =new Intent(this,typeof(RedirectClass));
// i.PutExtra("key",selectedFromList);
// StartActivity(i);
//int pos=Convert.ToInt32(position);
//ListView Clicked item value
//string itemValue =(string)listView.GetItemAtPosition(pos);
//Toast.MakeText(this," position is " +itemValue,ToastLength.Long).Show();
};
}
}
second activity to get data
TextView txt = FindViewById<TextView> (Resource.Id.textView1);
// Intent intent = Intent.getIntent();
string str = Intent.GetStringExtra("key").ToString();
Toast.MakeText (this, " the data is recvied from main is.." + str, ToastLength.Long).Show ();
txt.Text = str.ToString ();
source to share