android - Get index position of element inside a inflated view -
i trying index of spinner inside inflated view.
although i'm successful in retrieving index of child view when it's inflated on button click this:
itinerarydetailll = (linearlayout) findviewbyid(r.id.itinerarydetaillinearlayout); childview = getlayoutinflater().inflate(r.layout.cardview, null); itinerarydetailll.addview(childview); int posvalue = itinerarydetailll.indexofchild(childview); toast.maketext ( planitinerarydetailview.this, integer.tostring(posvalue), toast.length_short ).show();
this bit of code returns me 0
1st inflated view, 1
2nd inflated view , on...
problem
but when i'm trying index position of spinner (by calling setontouchlistener
event) in each of child view returns me last index position each spinner
spinnerplanitinerary.setontouchlistener(new view.ontouchlistener(){ @override public boolean ontouch(view v, motionevent event) { int indexvaluetown = itinerarydetailll.indexofchild(childview); toast.maketext ( planitinerarydetailview.this, integer.tostring(indexvaluetown), toast.length_short ).show(); return true; } });
this returns me 1
spinner in 1st inflated view, 1
spinner 2nd inflated view (it should return 0
spinner in 1st inflated view, 1
spinner in 2nd inflated view).
i hope i'm clear problem.
note i'm inflating 2 views testing right now.
posting answer stumble here might solution.
the problem in case itinerarydetailll
& childview
global variables. means whenever add new item via button click, there value updating last inflated view. whenever touch on spinner
, getting last index expected.
so making them local & final
variable made sure refer same view when touch spinner.
i didn't understood requirement or trying though. there might better way of achieving this.
Comments
Post a Comment