Android - CheckedTextView 检查错误的元素
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.country_row, null);
}
Country item = getItem(position);
TextView tv1 = (TextView) v.findViewById(R.id.country);
final CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.checked);
ctv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ctv.toggle();
Country country = (Country) listView.getItemAtPosition(position);
Log.d("daim", country.getCountry() + " " + country.getCode());
//listView.getCheckItemIds();
}
});
tv1.setText(item.getCountry());
return v;
}
当我检查第一个元素(位置 0)时,它会被检查,但是当我向下滚动列表视图时,下一个“部分”的第一个位置也会被检查,这会一直向下继续。例如,我有 100 个元素,我检查位置 0,然后位置 10、20、30、40 等也被检查。如何处理这种问题?
提前致谢。
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.country_row, null);
}
Country item = getItem(position);
TextView tv1 = (TextView) v.findViewById(R.id.country);
final CheckedTextView ctv = (CheckedTextView) v.findViewById(R.id.checked);
ctv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
ctv.toggle();
Country country = (Country) listView.getItemAtPosition(position);
Log.d("daim", country.getCountry() + " " + country.getCode());
//listView.getCheckItemIds();
}
});
tv1.setText(item.getCountry());
return v;
}
When I check let's say first element (position 0), it gets checked, but when I scroll down the listview, the first position of next "section" gets also checked, this continues all the way down. For instance, I have 100 elements, and I check position 0, then position 10, 20, 30, 40 etc gets also checked. How can handle this kind of problem?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是因为列表视图回收其视图,因此它保留了可重用的视图池。我通常使用的模式是:
that's because the listview recycle its view, so it keeps a pool of view that reuse. THe pattern typically I use is: