如何使用 listview.GetItemAtPosition(e.Position) 获取用户点击的 ListView 项目的数据? [英] How to get the data of a ListView item the user has clicked on with listview.GetItemAtPosition(e.Position)?

查看:25
本文介绍了如何使用 listview.GetItemAtPosition(e.Position) 获取用户点击的 ListView 项目的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Xamarin.Android 应用程序中,我有一个之前填充的 ListView.在正确触发的 listview_ItemClick 事件处理程序中,我想检索单击的列表视图项视图的文本,以便在 AlertDialog 中显示有关它的一些信息.

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.

我的代码不起作用,因为我只能得到一个 Java.Lang.Object (lvItem),我不知道如何从中提取我需要的视图.

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

如果我像这样运行它,Toast 会打印:

If I run it like this, the Toast prints:

您单击了 System.Collections.Generic.Dictionary'2[System.String,System.String]

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

请注意,每个 ListView 行由两个 TextView 组成,我通过将 List> 传递给 ListView Adapter 构造函数来填充 ListView.

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.

简单来说:

列表视图

|文本视图1 |文本视图2 |---> 项目 1

| textView1 | textView2 | ---> item 1

|文本视图1 |文本视图2 |---> 项目 2

| textView1 | textView2 | ---> item 2

如果用户点击 ListView 的 item2,我需要检索 item 2 的 textView1.Text 或 textView2.Text.

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

我也想过这样做:

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

但是适配器 GetItemIdAtPosition 返回一个 long 而不是一个 int,所以我不确定它是同一种 Id.

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.

以下也不起作用(我认为它与 lv.GetItemAtPosition(e.position) 相同:

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

lv.Adapter.GetItem(e.Position);

推荐答案

也许是这样?

lvItem = lv.GetItemAtPosition(e.Position);

虽然我不确定它是否适用于 java 对象,但也许尝试在此线程的帮助下投射此对象?https://forums.xamarin.com/discussion/7894/how-to-convert-a-type-to-java-lang-object

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

Java.Lang.Object 转换为 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;
    }
}

这篇关于如何使用 listview.GetItemAtPosition(e.Position) 获取用户点击的 ListView 项目的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆