2

I'm using VS 2012 working on Android app. Actually, I want to achieve this:

// 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));
    }
});

But, I'm not getting setOnItemClickListener() Event. Reason is, I'm working in C# using Xamarin. I want to get select value or item of ListView. How I can do this?

1
  • don't forget to mark the right answer that helps you Commented Nov 3, 2015 at 11:47

3 Answers 3

13

Have your activity implement ListView.IOnItemClickListener like this:

public class SomeActivity: Activity, ListView.IOnItemClickListener

Get a refence to your listview like this:

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

Then set the OnItemClickListner to the activity since we are going to implement the interface ListView.IOnItemClickListener

lstItems.OnItemClickListener = this;

Finally add this to your activity class:

public void OnItemClick(AdapterView parent, View view, int position, long id)
{
//whatever you need it to do goes here.
}
Sign up to request clarification or add additional context in comments.

Comments

4

Here is the complete code snippet:

    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);
        };

    }

1 Comment

I came up with something similar for the ItemClick even but I can't understand what it's not working in my case... can you please take a look at stackoverflow.com/questions/36491960/… Thanks!
3

You can use ItemClick and a delegate for put your event code, like this:

lv.ItemClick += delegate(object sender, Android.Widget.AdapterView.ItemClickEventArgs e)
{
    //Your code here
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.