AirTayork AirTayork - 1 month ago 11
Android Question

Android ListViewAdapter return wrong position after adding more data and invoking setSelection() method

I currently lost in a problem that I have a list which displays ImageViews when the ListView scroll to the top I will add more data to the list and make the ListView still show the current image instead of the first image from the list. So I use the setSelection method after data changing and the invoking of notifyDatasetHasChanged().But when I scroll the List one more time after the above invokes, I get wrong position in the getView() method from the adapter.

Here is the code:

for (int i = tempPics.size() - 1; i >= 0; i--) {
DBChapterPicture pic = (DBChapterPicture) tempPics.get(i);
chapterPics.add(0, pic);
}

detailAdapter.notifyDataSetChanged();
detailListView.setSelection(chapterPics.size() - headState - 1);


And after this piece executed in the getView() of my adapter I got wrong position:

@Override
public View getView(final int position, View convertView, ViewGroup parent)
{}


I don't know why. I think it may caused by the setSelection() method.

Thanks in advance.

Answer

It's been solved.I set the ImageView's size before the image data has loaded and the height I set is very large, as large as five times of the screen height.So the ListView only create one cell when the adapter is set to it.This caused the wrong position problem.