问题
I have a realization of ArrayAdapter
(extends ArrayAdapter
), the method getView constantly redraw the screen,constantly changing position item of the ListView.
But this is not the only problem. When I want to make text changes - are re-sorted. As well as to activate the line by setOnClickListener
I have to click twice
My source
public View getView(int position, View convertView, ViewGroup parent) {
UrlItem urlItem = (UrlItem) this.getItem(position);
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.database_table_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.editText1);
viewHolder.text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int getPosition = (Integer) v.getTag();
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.editText1, viewHolder.text);
viewHolder.text.setTag(position);
viewHolder.text.setText(urlItem.getUrl());
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
return convertView;
}
Helped me to this link.
回答1:
You're not doing anything with your views when they are recycled (i.e. when convertView != null). You need to do any position-specific stuff after obtaining the viewHolder. Try something more along these lines:
if (convertView == null) {
convertView = inflater.inflate(R.layout.database_table_item, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) convertView
.findViewById(R.id.editText1);
viewHolder.text.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int getPosition = (Integer) v.getTag();
}
});
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.text.setTag(position);
viewHolder.text.setText(urlItem.getUrl());
return convertView;
来源:https://stackoverflow.com/questions/11953875/getview-the-constant-change-of-position-of-items-and-i-have-to-click-twice-o