Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug : was crashing when removing an item from a list and adding another one of a different type #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions GreenDroid/src/greendroid/widget/ItemAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ private static class TypeInfo {
int type;
}

private List<Item> mItems;
private HashMap<Class<? extends Item>, TypeInfo> mTypes;
private Context mContext;
private final List<Item> mItems;
private final HashMap<Class<? extends Item>, TypeInfo> mTypes;
private final Context mContext;

private boolean mNotifyOnChange;
private int mMaxViewTypeCount;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends Item> getItemClass() {
return TextItem.class;
}

}
13 changes: 11 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/DrawableItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -62,4 +66,9 @@ public void setObject(Item object) {
}
}

@Override
public Class<? extends Item> getItemClass() {
return DrawableItem.class;
}

}
2 changes: 2 additions & 0 deletions GreenDroid/src/greendroid/widget/itemview/ItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,6 @@ public interface ItemView {
*/
void setObject(Item item);

Class<? extends Item> getItemClass();

}
12 changes: 10 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/LongTextItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Item> getItemClass() {
return LongTextItem.class;
}

}
12 changes: 10 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/ProgressItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends Item> getItemClass() {
return ProgressItem.class;
}

}
12 changes: 10 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/SeparatorItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Item> getItemClass() {
return TextItem.class;
}

}
13 changes: 11 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/SubtextItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Item> getItemClass() {
return SubtextItem.class;
}

}
12 changes: 10 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/SubtitleItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Item> getItemClass() {
return SubtitleItem.class;
}

}
12 changes: 10 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/TextItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<? extends Item> getItemClass() {
return TextItem.class;
}

}
12 changes: 10 additions & 2 deletions GreenDroid/src/greendroid/widget/itemview/ThumbnailItemView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends Item> getItemClass() {
return ThumbnailItem.class;
}
}