java - Changing visibility on button in custom adapter -
i have problem listview , custom adapters. im trying put visibility of button gone when click on it, create new element list edit , change.
but doesn't work. think because notifyondatachange puts buttons visibility visible again.
public class customadapteringredientsuser extends arrayadapter<recipe.ingredients.ingredient> list<recipe.ingredients.ingredient> ingredientlist; context context; edittext textquantity; edittext textname; button xbutton; button plusbutton; public customadapteringredientsuser(context context, list<recipe.ingredients.ingredient> resource) { super(context,r.layout.ingredient_new_recipe,resource); this.context = context; this.ingredientlist = resource; } @nonnull @override public view getview(final int position, @nullable view convertview, @nonnull viewgroup parent) { layoutinflater layoutinflater = layoutinflater.from(getcontext()); final view customview = layoutinflater.inflate(r.layout.ingredient_new_recipe,parent,false); textquantity = (edittext) customview.findviewbyid(r.id.quantitytext); textname = (edittext) customview.findviewbyid(r.id.ingredientname); plusbutton= (button) customview.findviewbyid(r.id.newingredientbutton); xbutton = (button) customview.findviewbyid(r.id.xbutton); xbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ingredientlist.remove(position); notifydatasetchanged(); } }); plusbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { plusbutton.setvisibility(customview.gone); string name = textname.gettext().tostring(); string qnt = textquantity.gettext().tostring(); recipe.ingredients.ingredient ing2 = new recipe.ingredients.ingredient("quantity","name","photo"); ingredientlist.add(ing2); notifydatasetchanged(); } }); return customview; }
it should let add new elements list, , remove button add more elemenets, in first 1 (the plus button). let user make list of ingredients.
you correct; calling notifydatasetchanged()
going make button re-appear. why?
when call notifydatasetchanged()
, listview redraw itself, going adapter information needs. involves calling getview()
currently-visible items in list.
your implementation of getview()
inflates , returns customview
. since you're returning newly-inflated view, attributes of views not manually set after inflation set values in layout xml (or defaults, if not set here).
the default value visibility visible
, when inflate customview
, plusbutton
visible unless manually change gone
.
you have store sort of indication whether button @ given position
should visible or gone, , apply inside getview()
after inflate customview
.
ps: in general, bad idea inflate new view every time getview()
called. should leverage convertview
argument , "view holder" pattern. improve performance of app. searching google , site should give ideas this.
Comments
Post a Comment