Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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);

}
— asked November 11th 2010 by Jeroen van Gijzel
  • click
  • createview
  • event
  • id
  • mobile
  • view
0 Comments

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 :)

    — answered November 11th 2010 by Dan Tamas
    permalink
    0 Comments
  • 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.

    — answered November 11th 2010 by Jeroen van Gijzel
    permalink
    2 Comments
    • Jeroen

      Firstly, this is not an answer - it should have been posted as a comment response to Tamas' answer.

      Tamas' code looks sound to me. Please gist your code, so that we can check it.

      — commented November 11th 2010 by Paul Dowsett
    • ^^^ the full script, not just a snippet.

      — commented November 11th 2010 by Paul Dowsett
  • you can set id:1 or any integer value and then get on click by e.source.id.
    This will helped me

    — answered May 24th 2013 by rajiv kumar
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.