2

我正在使用 VS 2012 开发 Android 应用程序。实际上,我想实现这一点:

// lv = My listView Name. 
lv.setOnItemClickListener(new OnItemClickListener() 
{ 
    public void onItemClick(AdapterView<?> myAdapter, View myView, int myItemInt, long mylng) 
    {      
        String selectedFromList =(String) (lv.getItemAtPosition(myItemInt));
    }
});

但是,我没有收到setOnItemClickListener()事件。原因是,我正在使用 C# 使用Xamarin工作。我想获取ListView的选择值或项目。我怎么能做到这一点?

4

3 回答 3

13

让您的活动像这样实现 ListView.IOnItemClickListener:

public class SomeActivity: Activity, ListView.IOnItemClickListener

像这样引用您的列表视图:

LsitView LV = FindViewById<ListView>(Resource.Id.id_in_axml);

然后将 OnItemClickListner 设置为活动,因为我们要实现接口 ListView.IOnItemClickListener

lstItems.OnItemClickListener = this;

最后将其添加到您的活动类中:

public void OnItemClick(AdapterView parent, View view, int position, long id)
{
//whatever you need it to do goes here.
}
于 2013-10-16T20:33:32.580 回答
4

这是完整的代码片段:

    private List<String>namess;
    private ListView listvieww;
    protected override void OnCreate (Bundle bundle)
    {
        base.OnCreate (bundle);

        SetContentView (Resource.Layout.Main);

        listvieww = FindViewById<ListView> (Resource.Id.listView1);


        namess = new List<String>();

        namess.Add("Security Solution");
        namess.Add("software");
        namess.Add("hardware");
        ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, Android.Resource.Layout.SimpleListItem1,namess);

        listvieww.Adapter = adapter;
        //listvieww.ItemClick += listView_ItemClick;


        listvieww.ItemClick += (object sender, Android.Widget.AdapterView.ItemClickEventArgs e) =>
        {
            string selectedFromList = listvieww.GetItemAtPosition(e.Position).ToString();

            Console.WriteLine( selectedFromList);
        };

    }
于 2015-06-08T10:43:03.273 回答
3

您可以使用 ItemClick 和委托来放置您的事件代码,如下所示:

lv.ItemClick += delegate(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
    //Your code here
}
于 2015-02-06T02:26:57.677 回答