From e5326940fdb300962cb92da5f746e807cb374975 Mon Sep 17 00:00:00 2001 From: "clement.sciascia" Date: Thu, 20 Oct 2011 16:44:17 +0200 Subject: [PATCH] Fixed bug : was crashing when removing an item from a list and adding another one of a different type --- .../src/greendroid/widget/ItemAdapter.java | 20 +++++++++++-------- .../widget/itemview/DescriptionItemView.java | 8 +++++++- .../widget/itemview/DrawableItemView.java | 13 ++++++++++-- .../greendroid/widget/itemview/ItemView.java | 2 ++ .../widget/itemview/LongTextItemView.java | 12 +++++++++-- .../widget/itemview/ProgressItemView.java | 12 +++++++++-- .../widget/itemview/SeparatorItemView.java | 12 +++++++++-- .../widget/itemview/SubtextItemView.java | 13 ++++++++++-- .../widget/itemview/SubtitleItemView.java | 12 +++++++++-- .../widget/itemview/TextItemView.java | 12 +++++++++-- .../widget/itemview/ThumbnailItemView.java | 12 +++++++++-- 11 files changed, 103 insertions(+), 25 deletions(-) diff --git a/GreenDroid/src/greendroid/widget/ItemAdapter.java b/GreenDroid/src/greendroid/widget/ItemAdapter.java index d7c12bb..ab97dd8 100644 --- a/GreenDroid/src/greendroid/widget/ItemAdapter.java +++ b/GreenDroid/src/greendroid/widget/ItemAdapter.java @@ -76,9 +76,9 @@ private static class TypeInfo { int type; } - private List mItems; - private HashMap, TypeInfo> mTypes; - private Context mContext; + private final List mItems; + private final HashMap, TypeInfo> mTypes; + private final Context mContext; private boolean mNotifyOnChange; private int mMaxViewTypeCount; @@ -403,15 +403,18 @@ public static ItemAdapter createFromXml(Context context, XmlPullParser parser) t return new ItemAdapter(context, items); } - public int getCount() { + @Override + public int getCount() { return mItems.size(); } - public Object getItem(int position) { + @Override + public Object getItem(int position) { return mItems.get(position); } - public long getItemId(int position) { + @Override + public long getItemId(int position) { return position; } @@ -430,12 +433,13 @@ public int getViewTypeCount() { return mMaxViewTypeCount; } - public View getView(int position, View convertView, ViewGroup parent) { + @Override + public View getView(int position, View convertView, ViewGroup parent) { final Item item = (Item) getItem(position); ItemView cell = (ItemView) convertView; - if (cell == null) { + if (cell == null || cell.getItemClass() != item.getClass()) { cell = item.newView(mContext, null); cell.prepareItemView(); } diff --git a/GreenDroid/src/greendroid/widget/itemview/DescriptionItemView.java b/GreenDroid/src/greendroid/widget/itemview/DescriptionItemView.java index 6181c91..d2be2af 100644 --- a/GreenDroid/src/greendroid/widget/itemview/DescriptionItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/DescriptionItemView.java @@ -41,11 +41,17 @@ public DescriptionItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } - public void prepareItemView() { + @Override + public void prepareItemView() { } public void setObject(Item item) { setText(((TextItem) item).text); } + @Override + public Class getItemClass() { + return TextItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/DrawableItemView.java b/GreenDroid/src/greendroid/widget/itemview/DrawableItemView.java index 4fcf65b..ca2e1d4 100644 --- a/GreenDroid/src/greendroid/widget/itemview/DrawableItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/DrawableItemView.java @@ -17,6 +17,8 @@ import greendroid.widget.item.DrawableItem; import greendroid.widget.item.Item; +import greendroid.widget.item.TextItem; + import android.content.Context; import android.util.AttributeSet; import android.view.View; @@ -44,12 +46,14 @@ public DrawableItemView(Context context, AttributeSet attrs) { super(context, attrs); } - public void prepareItemView() { + @Override + public void prepareItemView() { mTextView = (TextView) findViewById(R.id.gd_text); mImageView = (ImageView) findViewById(R.id.gd_drawable); } - public void setObject(Item object) { + @Override + public void setObject(Item object) { final DrawableItem item = (DrawableItem) object; mTextView.setText(item.text); @@ -62,4 +66,9 @@ public void setObject(Item object) { } } + @Override + public Class getItemClass() { + return DrawableItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/ItemView.java b/GreenDroid/src/greendroid/widget/itemview/ItemView.java index bba56a0..93076ad 100644 --- a/GreenDroid/src/greendroid/widget/itemview/ItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/ItemView.java @@ -48,4 +48,6 @@ public interface ItemView { */ void setObject(Item item); + Class getItemClass(); + } diff --git a/GreenDroid/src/greendroid/widget/itemview/LongTextItemView.java b/GreenDroid/src/greendroid/widget/itemview/LongTextItemView.java index 4c7e04f..87ba4a5 100644 --- a/GreenDroid/src/greendroid/widget/itemview/LongTextItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/LongTextItemView.java @@ -15,6 +15,7 @@ */ package greendroid.widget.itemview; +import greendroid.widget.item.DrawableItem; import greendroid.widget.item.Item; import greendroid.widget.item.LongTextItem; import android.content.Context; @@ -40,11 +41,18 @@ public LongTextItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } - public void prepareItemView() { + @Override + public void prepareItemView() { } - public void setObject(Item item) { + @Override + public void setObject(Item item) { setText(((LongTextItem) item).text); } + @Override + public Class getItemClass() { + return LongTextItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/ProgressItemView.java b/GreenDroid/src/greendroid/widget/itemview/ProgressItemView.java index ae64138..aba8748 100644 --- a/GreenDroid/src/greendroid/widget/itemview/ProgressItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/ProgressItemView.java @@ -16,6 +16,7 @@ package greendroid.widget.itemview; import greendroid.widget.item.Item; +import greendroid.widget.item.LongTextItem; import greendroid.widget.item.ProgressItem; import android.content.Context; import android.util.AttributeSet; @@ -48,15 +49,22 @@ public ProgressItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } - public void prepareItemView() { + @Override + public void prepareItemView() { mProgressBar = (ProgressBar) findViewById(R.id.gd_progress_bar); mTextView = (TextView) findViewById(R.id.gd_text); } - public void setObject(Item object) { + @Override + public void setObject(Item object) { final ProgressItem item = (ProgressItem) object; mProgressBar.setVisibility(item.isInProgress ? View.VISIBLE : View.GONE); mTextView.setText(item.text); } + @Override + public Class getItemClass() { + return ProgressItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/SeparatorItemView.java b/GreenDroid/src/greendroid/widget/itemview/SeparatorItemView.java index 7e9a61a..22b560e 100644 --- a/GreenDroid/src/greendroid/widget/itemview/SeparatorItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/SeparatorItemView.java @@ -16,6 +16,7 @@ package greendroid.widget.itemview; import greendroid.widget.item.Item; +import greendroid.widget.item.ProgressItem; import greendroid.widget.item.SeparatorItem; import greendroid.widget.item.TextItem; import android.content.Context; @@ -41,12 +42,19 @@ public SeparatorItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } - public void prepareItemView() { + @Override + public void prepareItemView() { } - public void setObject(Item object) { + @Override + public void setObject(Item object) { final TextItem item = (TextItem) object; setText(item.text); } + @Override + public Class getItemClass() { + return TextItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/SubtextItemView.java b/GreenDroid/src/greendroid/widget/itemview/SubtextItemView.java index b9e5885..3f909ff 100644 --- a/GreenDroid/src/greendroid/widget/itemview/SubtextItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/SubtextItemView.java @@ -17,6 +17,8 @@ import greendroid.widget.item.Item; import greendroid.widget.item.SubtextItem; +import greendroid.widget.item.TextItem; + import android.content.Context; import android.util.AttributeSet; import android.widget.LinearLayout; @@ -42,15 +44,22 @@ public SubtextItemView(Context context, AttributeSet attrs) { super(context, attrs); } - public void prepareItemView() { + @Override + public void prepareItemView() { mTextView = (TextView) findViewById(R.id.gd_text); mSubtextView = (TextView) findViewById(R.id.gd_subtext); } - public void setObject(Item object) { + @Override + public void setObject(Item object) { final SubtextItem item = (SubtextItem) object; mTextView.setText(item.text); mSubtextView.setText(item.subtext); } + @Override + public Class getItemClass() { + return SubtextItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/SubtitleItemView.java b/GreenDroid/src/greendroid/widget/itemview/SubtitleItemView.java index 424b2e2..79d20ac 100644 --- a/GreenDroid/src/greendroid/widget/itemview/SubtitleItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/SubtitleItemView.java @@ -16,6 +16,7 @@ package greendroid.widget.itemview; import greendroid.widget.item.Item; +import greendroid.widget.item.SubtextItem; import greendroid.widget.item.SubtitleItem; import android.content.Context; import android.util.AttributeSet; @@ -42,15 +43,22 @@ public SubtitleItemView(Context context, AttributeSet attrs) { super(context, attrs); } - public void prepareItemView() { + @Override + public void prepareItemView() { mTextView = (TextView) findViewById(R.id.gd_text); mSubtitleView = (TextView) findViewById(R.id.gd_subtitle); } - public void setObject(Item object) { + @Override + public void setObject(Item object) { final SubtitleItem item = (SubtitleItem) object; mTextView.setText(item.text); mSubtitleView.setText(item.subtitle); } + @Override + public Class getItemClass() { + return SubtitleItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/TextItemView.java b/GreenDroid/src/greendroid/widget/itemview/TextItemView.java index ff6bd93..468d885 100644 --- a/GreenDroid/src/greendroid/widget/itemview/TextItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/TextItemView.java @@ -16,6 +16,7 @@ package greendroid.widget.itemview; import greendroid.widget.item.Item; +import greendroid.widget.item.SubtitleItem; import greendroid.widget.item.TextItem; import android.content.Context; import android.util.AttributeSet; @@ -40,11 +41,18 @@ public TextItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } - public void prepareItemView() { + @Override + public void prepareItemView() { } - public void setObject(Item object) { + @Override + public void setObject(Item object) { setText(((TextItem) object).text); } + @Override + public Class getItemClass() { + return TextItem.class; + } + } diff --git a/GreenDroid/src/greendroid/widget/itemview/ThumbnailItemView.java b/GreenDroid/src/greendroid/widget/itemview/ThumbnailItemView.java index 4aa1703..b0e80a2 100644 --- a/GreenDroid/src/greendroid/widget/itemview/ThumbnailItemView.java +++ b/GreenDroid/src/greendroid/widget/itemview/ThumbnailItemView.java @@ -17,6 +17,7 @@ import greendroid.widget.AsyncImageView; import greendroid.widget.item.Item; +import greendroid.widget.item.TextItem; import greendroid.widget.item.ThumbnailItem; import android.content.Context; import android.util.AttributeSet; @@ -48,17 +49,24 @@ public ThumbnailItemView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } - public void prepareItemView() { + @Override + public void prepareItemView() { mTextView = (TextView) findViewById(R.id.gd_text); mSubtitleView = (TextView) findViewById(R.id.gd_subtitle); mThumbnailView = (AsyncImageView) findViewById(R.id.gd_thumbnail); } - public void setObject(Item object) { + @Override + public void setObject(Item object) { final ThumbnailItem item = (ThumbnailItem) object; mTextView.setText(item.text); mSubtitleView.setText(item.subtitle); mThumbnailView.setDefaultImageResource(item.drawableId); mThumbnailView.setUrl(item.drawableURL); } + + @Override + public Class getItemClass() { + return ThumbnailItem.class; + } }