How to get the data of a ListView item the user has clicked on with listview.GetItemAtPosition(e.Position)?

后端 未结 6 1820
温柔的废话
温柔的废话 2021-01-23 00:36

in a Xamarin.Android app, I have a ListView which I previously populated. In the listview_ItemClick event handler, which correctly fires, I would like to retrieve the text of t

相关标签:
6条回答
  • 2021-01-23 01:05

    I think you can do something like this ((TextView)e.View).getText().toString()

    0 讨论(0)
  • 2021-01-23 01:08

    You can have a Button that catch the Ítem Clicked...

    You can have the element Position like this.

    int position= FindViewById(Resource.Id.listIncindentes).CheckedItemPosition;

    And the element value like this.

    var value= FindViewById(Resource.Id.listIncindentes).GetItemAtPosition(position);

    0 讨论(0)
  • 2021-01-23 01:13

    Maybe with something like this?

    lvItem = lv.GetItemAtPosition(e.Position);
    

    Although im not sure it will work with java objects, maybe try to cast this object with the help of this thread? https://forums.xamarin.com/discussion/7894/how-to-convert-a-type-to-java-lang-object

    Converting Java.Lang.Object to C#:

    public static class ObjectTypeHelper
    {
        public static T Cast<T>(this Java.Lang.Object obj) where T : class
        {
            var propertyInfo = obj.GetType().GetProperty("Instance");
            return propertyInfo == null ? null : propertyInfo.GetValue(obj, null) as T;
        }
    }
    
    0 讨论(0)
  • 2021-01-23 01:20
    List<Dictionary<string,string>> lstItems;
    

    lstItems contains all the list item, which is loaded in the listview. From the listView click listener you will get the position of the selected item. Suppose it is i. Then you can take that item from lstItems.

    private void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {
    
        Dictionary<string,string> selectedItem=lstItems.get(e.Position);
        // toast necessary information using selectedItem object
    
    }
    
    0 讨论(0)
  • 2021-01-23 01:28

    try this it might help

    void OnListItemClick(object sender, AdapterView.ItemClickEventArgs e)
    {
    var listView = sender as ListView;
    var t = tableItems[e.Position];
    Android.Widget.Toast.MakeText(this, t.Heading,   
    Android.Widget.ToastLength.Short).Show();
    }
    
    0 讨论(0)
  • 2021-01-23 01:28

    Check the below code for use the Particular Textview and other view from Listview Items.

    Like :

    TextView mTextView = (TextView) listview.getChildAt(0).findViewById(R.id.text);

    You can easily get the list item of Listview.

    0 讨论(0)
提交回复
热议问题