问题描述
我使用的是自定义的BaseAdapter显示在一个ListView项目。该项目是在一个ArrayList刚刚举行的字符串。
I am using a customised BaseAdapter to display items on a ListView. The items are just strings held in an ArrayList.
列表项,对他们(大红色的X),删除按钮,我想从ArrayList中删除的项,并通知ListView的自我更新。
The list items have a delete button on them (big red X), and I'd like to remove the item from the ArrayList, and notify the ListView to update itself.
但是,每一个实施我试图得到赋予它神秘的位置编号,因此,例如点击第2项的删除按钮将删除第5项的。这似乎是几乎完全是随机的。
However, every implementation I've tried gets mysterious position numbers given to it, so for example clicking item 2's delete button will delete item 5's. It seems to be almost entirely random.
有一点要注意的是,元件可以被重复,但必须保持在相同的顺序。例如,我可以有爱尔兰的两倍,作为元素3和7。
One thing to note is that elements may be repeated, but must be kept in the same order. For example, I can have "Irish" twice, as elements 3 and 7.
我的code是如下:
private static class ViewHolder {
TextView lang;
int position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.language_link_row, null);
holder = new ViewHolder();
holder.lang = (TextView)convertView.findViewById(R.id.language_link_text);
holder.position = position;
final ImageView deleteButton = (ImageView)
convertView.findViewById(R.id.language_link_cross_delete);
deleteButton.setOnClickListener(this);
convertView.setTag(holder);
deleteButton.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.lang.setText(mLanguages.get(position));
return convertView;
}
我后来试图通过抓住标签检索已删除元素的位置,但它总是在列表中错误的位置。没有明显的模式来这里给出的位置,它似乎总是随机的。
I later attempt to retrieve the deleted element's position by grabbing the tag, but it's always the wrong position in the list. There is no noticeable pattern to the position given here, it always seems random.
// The delete button's listener
public void onClick(View v) {
ViewHolder deleteHolder = (ViewHolder) v.getTag();
int pos = deleteHolder.position;
...
...
...
}
我会很乐意刚刚从ArrayList中删除的项目,并有ListView的自我更新,但我得到的位置不正确,所以我不能做到这一点。
I would be quite happy to just delete the item from the ArrayList and have the ListView update itself, but the position I'm getting is incorrect so I can't do that.
请注意,我做了,起初,有deleteButton clickListener的getView方法中,并用位置可删除该值,但我有同样的问题。
Please note that I did, at first, have the deleteButton clickListener inside the getView method, and used 'position' to delete the value, but I had the same problem.
任何建议AP preciated,这实在是让我感到愤怒。
Any suggestions appreciated, this is really irritating me.
推荐答案
您必须每次设置的位置。你的实现只设置在创建视图的位置。该视图被再循环然而时(当convertView不为空),位置将不会被设置为正确的值。
You have to set the position each time. Your implementation only sets the position on the creation of the view. However when the view is recycled (when convertView is not null), the position will not be set to the correct value.
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.language_link_row, null);
holder = new ViewHolder();
holder.lang = (TextView)convertView.findViewById(R.id.language_link_text);
final ImageView deleteButton = (ImageView)
convertView.findViewById(R.id.language_link_cross_delete);
deleteButton.setOnClickListener(this);
convertView.setTag(holder);
deleteButton.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.lang.setText(mLanguages.get(position));
holder.position = position;
return convertView;
}
这篇关于从一个ListView使用自定义的BaseAdapter删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!