如何得到一个ListView项用户点击与listview.GetItemAtPosition(e.Position)的数据? [英] How to get the data of a ListView item the user has clicked on with listview.GetItemAtPosition(e.Position)?
问题描述
在Xamarin.Android应用程序,我有我以前填充一个ListView。
在listview_ItemClick事件处理程序,它正确触发,我想找回的点击列表视图项意见的文本,以显示其在AlertDialog一些信息。
我的代码无法正常工作,因为所有我能得到一个的java.lang.Object(LVITEM),我不知道如何从中提取我需要的意见。
私人无效lv_ItemClick(对象发件人,AdapterView.ItemClickEventArgs E){
LVITEM = lv.GetItemAtPosition(e.Position);
// LVITEM是的java.lang.Object
Toast.MakeText(这一点,+你点击+ LVITEM,ToastLength.Long).Show();
}
如果我运行它这样的,吐司打印:
您点击System.Collections.Generic.Dictionary'2 [System.String,
System.String]
< /块引用>
请注意,每个ListView的行由两个TextViews,我通过传递
列表与LT填充的ListView;字典<字符串,字符串>>
到ListView适配器构造
要说得简单:
LISTVIEW
| textView1 | textView2 | --->项目1
| textView1 | textView2 | --->项目2
如果用户点击的ListView ITEM2我需要检索textView1.Text或textView2.Text 2项。
我也想过这样做的:
FindViewById< TextView的>(resultsListView.GetItemIdAtPosition(e.Position );
但适配器GetItemIdAtPosition返回,而不是长期的INT,所以我不知道这是同一种标识的。
下面是不是工作压力太大(我认为这是同一个
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
转换的java.lang.Object 为 C#
公共静态类ObjectTypeHelper
{
公共静态牛逼演员LT; T>(此的java.lang.Object OBJ)其中T:类
{
变种的PropertyInfo = obj.GetType()的getProperty(实例)。
返回的PropertyInfo == NULL?空:propertyInfo.GetValue(OBJ,NULL)为T;
}
}
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; } }
这篇关于如何得到一个ListView项用户点击与listview.GetItemAtPosition(e.Position)的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!