From aff9bec92b9d81f186fa524bc3eeaa5094f146d5 Mon Sep 17 00:00:00 2001 From: cketti Date: Mon, 18 Mar 2013 22:56:56 +0100 Subject: [PATCH] Work around a bug in ListView --- .../fsck/k9/view/K9PullToRefreshListView.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/com/fsck/k9/view/K9PullToRefreshListView.java b/src/com/fsck/k9/view/K9PullToRefreshListView.java index 58a1024bb..5909bc624 100644 --- a/src/com/fsck/k9/view/K9PullToRefreshListView.java +++ b/src/com/fsck/k9/view/K9PullToRefreshListView.java @@ -1,6 +1,7 @@ package com.fsck.k9.view; import android.content.Context; +import android.graphics.Canvas; import android.os.Parcelable; import android.util.AttributeSet; import android.widget.ListView; @@ -28,6 +29,31 @@ public class K9PullToRefreshListView extends PullToRefreshListView { */ layoutChildren(); } + + @Override + protected void dispatchDraw(Canvas canvas) { + if (getAdapter() != null) { + int count = getChildCount(); + int itemCount = getAdapter().getCount(); + + /* + * 2013-03-18 - cketti + * + * Work around a bug in ListView (?) that leads to a crash deep inside the + * framework code. + * I didn't track down the exact cause of this. My best guess is that we change + * the data (remove items) while the ListView isn't visible. Probably because + * the view is hidden, layoutChildren() is never called and when this method + * runs the layout contains more item views than the adapter contains elements + * and bad things(tm) happen. + */ + if (itemCount < count) { + layoutChildren(); + } + } + + super.dispatchDraw(canvas); + } }; } }