如何在Andoid ListView中获取选定的项目索引?
问题描述:
大家好:)
我有一个列表视图,我想获取选定的项目索引以对其进行一些处理
如何获得此索引为整数值?
这是我的代码:
Hi all :)
I have a listview, and I want to get the selected item index to do some process on it
how can I get this index as an integer value ?
this is my code:
final ListView list = (ListView)findViewById(android.R.id.list);
list.setClickable(true);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
}
});
谢谢:)
thanks all :)
答
阅读文档.您不是第一个愿意发现...的人.
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html [^ ]
http://developer.android.com/reference/android/widget/ListView.html [ ^ ]
有关使用ListView的教程:
http://developer.android.com/resources/tutorials/views/hello-listview.html [^ ]
Read the documentation. You are not the first person ever willing to find out...
http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html[^]
http://developer.android.com/reference/android/widget/ListView.html[^]
Tutorial on using ListView:
http://developer.android.com/resources/tutorials/views/hello-listview.html[^]
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
}
int位置将为您提供int中的选定项目索引.
The int position will give you the selected item index in int.
我的用户如下所示,工作正常
i user like below and working nice
mylistview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
MyObject selection = (MyObject) arg0.getItemAtPosition(arg2);
}
});
不同的是
我没有使用
the different is
i havent use
list.setClickable(true);
并放置
@Override
属性