本文介绍了获取有关OnItemClick ListView项的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图让在一个列表视图自定义适配器所选项目的价值。我尝试用以下code:
I try to get the value of a selected Item within a custom adapter on a listview. I try this with following code:
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
View curr = parent.getChildAt((int) id);
TextView c = (TextView)curr.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
在开始的时候,如果我点击,该值是好的,但一旦我滚动,我点击另一个项目,我得到的点击项目...任何想法是什么原因造成这种错误的价值?
At the beginning, if I click, the values are good, but once I scrolled and I click on another Item, I get the wrong value of that clicked item... Any idea what is causing this?
推荐答案
参数 v
是当前行。所以使用:
The parameter v
is the current row. so use:
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView c = (TextView) v.findViewById(R.id.tvPopUpItem);
String playerChanged = c.getText().toString();
Toast.makeText(Settings.this,playerChanged, Toast.LENGTH_SHORT).show();
}
(或者你可以使用 getChildAt(位置)
但这会慢一些。)
了解您可能能够简化这更取决于你的布局。
Understand you might be able to simplify this more depending on your layout.
这篇关于获取有关OnItemClick ListView项的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!