简体   繁体   中英

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

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 the clicked listview-item views, in order to show some info about it in a AlertDialog.

My code is not working because all I can get to is a Java.Lang.Object (lvItem) and I don't know how to extract from it the Views I need.

private void lv_ItemClick(object sender, AdapterView.ItemClickEventArgs e) {

    lvItem = lv.GetItemAtPosition(e.Position);
    // lvItem is a Java.Lang.Object

    Toast.MakeText(this,  + "you clicked" + lvItem, ToastLength.Long).Show();
}

If I run it like this, the Toast prints:

you clicked System.Collections.Generic.Dictionary'2[System.String, System.String]

Note that each ListView row is made up of two TextViews, and I populate the ListView by passing a List<Dictionary<string,string>> to the ListView Adapter constructor.

To put it simpler:

LISTVIEW

| textView1 | textView2 | ---> item 1

| textView1 | textView2 | ---> item 2

If user clicks on item2 of ListView I need to retrieve textView1.Text or textView2.Text of item 2.

I also thought about doing:

FindViewById<TextView>(resultsListView.GetItemIdAtPosition(e.Position);

but the Adapter GetItemIdAtPosition returns a long instead of a int, and so I am not sure that it's the same kind of Id.

The following is not working too (I think it's the same of lv.GetItemAtPosition(e.position) :

lv.Adapter.GetItem(e.Position);

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

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

}

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

我认为你可以做这样的事情((TextView)e.View).getText().toString()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM