如何在xamarin android上选择listview时获取项目?

时间:2016-04-21 15:39:25

标签: listview xamarin xamarin.android selecteditem

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?
}

如何从Xamarin.Android上的ListView获取所选项目并创建一个返回旧片段的后退按钮?

3 个答案:

答案 0 :(得分:1)

ItemClickEventArgs Position会告诉您所选项目的索引

lst.ItemClick += delegate(object sender, AdapterView.ItemClickEventArgs e) {

  var selected = items[e.Position];
}

答案 1 :(得分:0)

OnListItemClick子类中覆盖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 (); } } 以接收点击的项目

    {"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53453/api/nisperson/addnewconnection'.",
"MessageDetail":"No action was found on the controller 'NISPerson' that matches the request."}
  

Android.App.ListFragment.OnListItemClick方法

     

当选择列表中的项目时,将调用此方法。

https://developer.xamarin.com/api/member/Android.App.ListFragment.OnListItemClick/p/Android.Widget.ListView/Android.Views.View/System.Int32/System.Int64/

答案 2 :(得分:0)

只需为ListView.ItemClick

创建一个方法
mListView.ItemClick += MListView_ItemClick;

从方法中可以检索点击项目的索引

void MListView_ItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
        Toast.MakeText(Application.Context, e.Position, ToastLength.Short).Show();
    }