How to give an id to a view and get it back from the click event
Hello,
I have a list of items, each item is represented by a "View" created with createView in a loop.
Where can i set the id to this view and how can i get it back from the click event?
Here are some parts of my code
// The item
var itemView = Titanium.UI.createView{(
width:320,
height:60,
top:0,
left:0,
backgroundImage:'images/background_item.png'
)};
// Eventlistener
itemView.addEventListener('click',function(e) {
itemId = e. ????;
Titanium.API.info('user clicked on item: ' + itemId);
}
3 Answers
-
Accepted Answer
// The item var itemView = Titanium.UI.createView{( width:320, height:60, top:0, left:0, backgroundImage:'images/background_item.png', my_id:'the_id_of_this_view' )}; // Eventlistener itemView.addEventListener('click',function(e) { itemId = e.source.my_id Titanium.API.info('user clicked on item: ' + itemId); }
don't use id, it seems that is used internally by Titanium so you might break stuff :)
-
I tried your suggestion, the trace tells me "user clicked on item: undefined".
edit:
when i try to retrieve another property like "height" with e.source.height then i do get back the right value. -
you can set id:1 or any integer value and then get on click by e.source.id.
This will helped me