Reputation: 381
string[] items = new string[] { "Alternative Rock","Classical"}
lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {
FragmentTransaction fragmentTx=this.FragmentManager.BeginTransaction();
TracksByGenres fragTrack=new TracksByGenres();
//get our item from listview
fragmentTx.Replace(Resource.Id.fragmentContainer,fragTrack,.....);
fragmentTx.AddToBackStack(null);
fragmentTx.Commit();
};
TracksByGenres.cs
public async override void OnActivityCreated(Bundle savedInstancesState)
{
base.OnActivityCreated (savedInstancesState);
// what do I write?
}
How can I get the selected item form the ListView
on Xamarin.Android and create a back button that will return old fragment?
Upvotes: 1
Views: 8010
Reputation: 484
simply create a method for ListView.ItemClick
mListView.ItemClick += MListView_ItemClick;
from within the method you can retrieve the index of clicked item
void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
{
Toast.MakeText(Application.Context, e.Position, ToastLength.Short).Show();
}
Upvotes: 0
Reputation: 89204
ItemClickEventArgs
Position
will tell you the index of the selected item
lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {
var selected = items[e.Position];
}
Upvotes: 1
Reputation: 74209
In your ListFragment
subclass override the OnListItemClick
to receive the item that the clicked on
public class myListFragment : ListFragment
{
string[] data = { "Alternative Rock","Classical" } ;
protected override void OnCreate (Bundle bundle)
{
base.OnCreate (bundle);
ArrayAdapter adapter = new ArrayAdapter (this,
Resource.Layout.TextViewItem, data);
ListAdapter = adapter;
}
protected override void OnListItemClick (ListView l, View v,
int position, long id)
{
base.OnListItemClick (l, v, position, id);
Toast.MakeText (this, data [position],
ToastLength.Short).Show ();
}
}
Android.App.ListFragment.OnListItemClick Method
This method will be called when an item in the list is selected.
Upvotes: 0