ListView控件onItemClick只得到第一个项目 [英] ListView onItemClick only gets the first item
问题描述
我试图让所选项目的文本,并将其显示在敬酒的消息。这是code我写的:
I'm trying to get the text of the selected item and show it in a toast message. This is the code I wrote:
final ListView lv = (ListView)findViewById(R.id.firstflightlist);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView c = (TextView) arg0.findViewById(arg1.getId());
String text = c.getText().toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}});
在ListView是一个单一的选择列表视图。当我点击列表中的任何项目,它始终显示在列表中的第一项。什么导致此?我怎样才能获得所选项目的文字?
The listview is a single choice listview. When I click on any item in the list, it always displays the first item of the list. What might be causing this? How can I get the selected item's text?
推荐答案
您不需要findViewById,你已经得到了你所点击的视图。
也只有findViewById发现该ID匹配的第一个项目,在列表视图中,你已经得到了很多具有相同ID的项目,所以它找到的第一个
you don't need to findViewById, you've got the view you clicked on. also findViewById only finds the first item that matches the id, and in a list view you've got a lot of items with the same id, so it finds the first one
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView c = (TextView) arg1; //<--this one
String text = c.getText().toString();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
}});
这篇关于ListView控件onItemClick只得到第一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!