Friday 15 April 2011

android - CursorAdapter with two row layouts -



android - CursorAdapter with two row layouts -

okey so, since i've not found consistent way of doing when searched, decided maybe putting code elses eyes see might help.

i'm creating game utilize cards. these cards can either locked or unlocked depending on different factors. of import thing is, want check sqlite db whether locked, display different row layouts 2 possible outcomes.

here's code:

public class allcardsadapter extends cursoradapter { lockedholder viewlocked; unlockedholder viewunlocked; private layoutinflater minflater; public static final int locked = 0; public static final int unlocked = 1; public allcardsadapter(context context, cursor cursor) { super(context, cursor); minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public void bindview(view view, context context, cursor cursor) { final int type; int viewtype = this.getitemviewtype(cursor.getint(cursor.getcolumnindex("unlocked"))); if (viewtype == 1){ type = unlocked; } else { type = locked; } if (type == locked){ viewlocked = (lockedholder) view.gettag(); viewlocked.nameholder.settext(cursor.getstring(cursor.getcolumnindex("name"))); viewlocked.awardedatholder.settext("awarded @ level:"); viewlocked.reqlvlholder.settext(cursor.getstring(cursor.getcolumnindex("reqlvl"))); string imagepath = cursor.getstring(cursor.getcolumnindex("image")); if (imagepath.equals("card_obj_plus_1")){ picasso.with(context).load(r.drawable.card_obj_plus_1).placeholder(r.drawable.card_placeholder).into(viewlocked.imageholder); } if (imagepath.equals("card_obj_plus_2")){ picasso.with(context).load(r.drawable.card_obj_plus_2).placeholder(r.drawable.card_placeholder).into(viewlocked.imageholder); } if (imagepath.equals("card_obj_plus_3")){ picasso.with(context).load(r.drawable.card_obj_plus_3).placeholder(r.drawable.card_placeholder).into(viewlocked.imageholder); } if (imagepath.equals("card_slowdown")){ picasso.with(context).load(r.drawable.card_slowdown).placeholder(r.drawable.card_placeholder).into(viewlocked.imageholder); } if (imagepath.equals("card_speed_up")){ picasso.with(context).load(r.drawable.card_speed_up).placeholder(r.drawable.card_placeholder).into(viewlocked.imageholder); } } else { viewunlocked = (unlockedholder) view.gettag(); viewunlocked.nameholder.settext(cursor.getstring(cursor.getcolumnindex("name"))); string imagepath = cursor.getstring(cursor.getcolumnindex("image")); if (imagepath.equals("card_obj_plus_1")){ picasso.with(context).load(r.drawable.card_obj_plus_1).placeholder(r.drawable.card_placeholder).into(viewunlocked.imageholder); } if (imagepath.equals("card_obj_plus_2")){ picasso.with(context).load(r.drawable.card_obj_plus_2).placeholder(r.drawable.card_placeholder).into(viewunlocked.imageholder); } if (imagepath.equals("card_obj_plus_3")){ picasso.with(context).load(r.drawable.card_obj_plus_3).placeholder(r.drawable.card_placeholder).into(viewunlocked.imageholder); } if (imagepath.equals("card_slowdown")){ picasso.with(context).load(r.drawable.card_slowdown).placeholder(r.drawable.card_placeholder).into(viewunlocked.imageholder); } if (imagepath.equals("card_speed_up")){ picasso.with(context).load(r.drawable.card_speed_up).placeholder(r.drawable.card_placeholder).into(viewunlocked.imageholder); } } } @override public view newview(context context, cursor cursor, viewgroup parent) { viewlocked = new lockedholder(); viewunlocked = new unlockedholder(); final int type; int viewtype = this.getitemviewtype(cursor.getint(cursor.getcolumnindex("unlocked"))); if (viewtype == 1){ type = unlocked; } else { type = locked; } system.out.println(viewtype); system.out.println(type); if (type == locked){ view lockedview; lockedview = minflater.inflate(r.layout.card_list_row_disabled, parent, false); viewlocked.nameholder = (textview) lockedview.findviewbyid(r.id.txttitle); viewlocked.imageholder = (imageview) lockedview.findviewbyid(r.id.imgthumbnail); viewlocked.reqlvlholder = (textview) lockedview.findviewbyid(r.id.tvlevelnr); viewlocked.awardedatholder = (textview) lockedview.findviewbyid(r.id.tvawardedat); lockedview.settag(viewlocked); homecoming lockedview; } else { view unlockedview; unlockedview = minflater.inflate(r.layout.card_list_row, parent, false); viewunlocked.nameholder = (textview) unlockedview.findviewbyid(r.id.txttitle); viewunlocked.imageholder = (imageview) unlockedview.findviewbyid(r.id.imgthumbnail); unlockedview.settag(viewunlocked); homecoming unlockedview; } } @override public int getviewtypecount() { homecoming 2; } @override public int getitemviewtype(int position) { homecoming position % 2; } public class lockedholder { public textview nameholder; public imageview imageholder; public textview awardedatholder; public textview reqlvlholder; } public class unlockedholder { public textview nameholder; public imageview imageholder; } }

the error i'm getting on row "viewlocked = (lockedholder) view.gettag();"

allcardsadapter$unlockedholder cannot cast allcardsadapter$lockedholder

the error shows when have list containing cards both locked , unlocked.

see know it's trying do, don't know why. also, if i'm overcomplicating things no reason, or missing obvious, i'd much appreciated if find it..

getitemviewtype implementation not right (type not depend on item position) should following

private int getitemviewtype(cursor cursor) { homecoming cursor.getint(cursor.getcolumnindex("unlocked")) % 2; } @override public int getitemviewtype(int position) { cursor cursor = (cursor) getitem(position); homecoming getitemviewtype(cursor); }

you need update bindview , newview

int viewtype = this.getitemviewtype(cursor);

android android-cursoradapter

No comments:

Post a Comment