Reputation: 221
My problem is that when I'm scrolling a list view I'm getting wrong convertView, but the position is right.
I have 3 items in my Listview, on load 'position' parameter is called with index '0' and convertView is null.
When I scroll one by one, to the next item 'position' is '1' and convertView is null also.
The problem is on item 3, the 'position' is '2' BUT the 'convertView' is not null, it is populated by the first item convertView.
Why is that ?
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.post_layout, parent, false);
postViewHolder = new PostViewHolder();
postViewHolder.commentsImageButton = (ImageButton) itemView.findViewById(R.id.postAddCommentsImageButton);
itemView.setTag(postViewHolder);
}else{
postViewHolder = (PostViewHolder) itemView.getTag();
}
}
Upvotes: 1
Views: 598
Reputation: 36
Try This
public View getView(final int position, View convertView, ViewGroup parent) {
View itemView = convertView;
PostViewHolder postViewHolder = null;
if (itemView == null) {
itemView = getLayoutInflater().inflate(R.layout.post_layout, parent, false);
postViewHolder = new PostViewHolder();
postViewHolder.commentsImageButton = (ImageButton) itemView.findViewById(R.id.postAddCommentsImageButton);
itemView.setTag(postViewHolder);
}else{
postViewHolder = (PostViewHolder) itemView.getTag();
}
postViewHolder.position = position;
:do your stuff with postViewHolder
:
return itemView;
}
Upvotes: 0
Reputation: 3776
You will only get N null items in your listView, this value N will change depending on how many rows enter in your screen.
For example if in your screen there are 3 rows, you will probably have 4 or 5 row items. This is because Android recycle the old views into new ones.
If you need to keep track of the holder position move your postViewHolder.position
out of your if / else statement.
Hope it helps.
Upvotes: 1