Reputation: 95
I'm facing the strangest problem which I've been unable to solve.
I have the following loop which its doing it's job correctly:
for (int i = 0; i < adapterCount; i++) {
View listItem = mListAdapter.getView(i, null, mListView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
However it is slow. To improve performance, and because every listItem will have the same size I want to put the first two lines outside the loop. This is how I tried:
View listItem = mListAdapter.getView(0, null, mListView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapterCount; i++) {
height += listItem.getMeasuredHeight();
}
However this gives me an exception at the first line:
java.lang.IllegalStateException: couldn't move cursor to position 0
Does anyone have any idea why that is?
Thank you!
Upvotes: 0
Views: 106
Reputation: 93561
What you're doing is totally counter to the entire idea of a listview. Calling getView like that would force it to create a row view for every row in the list. That's what listView is meant to avoid. Worse, you aren't even saving them. SO you're creating them for no purpose. This will never perform well, and you shouldn't be doing this at all- if you think you need to, you're designing your app wrong.
Upvotes: 1