Titanium Community Questions & Answer Archive

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

Dynamic view addeventlistener

What am I doing wrong here…
I create the view in a while that is reading from the db:

     var xeach = Ti.UI.createView({
                backgroundColor:'#FFFFA9',
                borderColor:'#000',
                borderWidth: 2,
                borderRadius: 5,
                zIndex:10,
                width:30,
                height:30,
                thefieldname: rows.fieldByName('fieldname'),
                thefieldid: rows.fieldByName('idNum'),
                top:toppos,
                left:280
                });

            view4b.add(xeach);

xeach.addEventListener('click', function(e)
{

        var thefieldnamenow=e.source.thefieldname;
        var thefieldidnow =e.source.thefieldid;

});

I get "null" value when I know that there is something there. There is a label right before that with rows.fieldByName('fieldname') and it has a value.

— asked November 25th 2010 by Stan Thompson
  • addeventlistener
  • dynamic
  • view
0 Comments

5 Answers

  • Hi Stan

    Place this just before the label to confirm that the resultset is returning data:

    Ti.API.info('rows.fieldByName('fieldname') = '+rows.fieldByName('fieldname'));
    Ti.API.info('rows.fieldByName('fieldname') = '+rows.fieldByName('idNum'));
    

    Inside the eventlistener, place this code at the start to confirm that the data is also available there too:

    Ti.API.info('e.source.thefieldname = '+e.source.thefieldname);
    Ti.API.info('e.source.thefieldname = '+e.source.thefieldid);
    

    Tell us the result. You may need to paste more of the code, if you can't get it working.

    — answered November 25th 2010 by Paul Dowsett
    permalink
    0 Comments
  • Thanks Hal…
    Results just as I thought-

    [INFO] [90,26066] rows.fieldByName('fieldname') = Test Name
    [INFO] [2,26068] rows.fieldByName('fieldname') = 4
    [INFO] [53,36267] e.source.thefieldname = null
    [INFO] [5,36272] e.source.thefieldname = null
    

    Any ideas

    — answered November 25th 2010 by Stan Thompson
    permalink
    1 Comment
    • Stan

      Note that you have used "e.source.thefieldname" twice.

      Hmm, that's strange - I can't see what is wrong, I'm afraid. :-/

      What mobile platform are you testing on? What mobile SDK are you using? What Titanium mobile SDK are you using?

      — commented November 25th 2010 by Paul Dowsett
  • This is on android. Just loaded and tested it with Titanium SDK version: 1.5.0, mobile sdk 2.2

    — answered November 25th 2010 by Stan Thompson
    permalink
    0 Comments
  • Hmmm, I have a label in the view and I change the addEventListener to the label and put the tags in the label and it worked. So I think it must be a strange bug or something.

    — answered November 25th 2010 by Stan Thompson
    permalink
    0 Comments
  • Stan

    !.5.X is not considered stable yet. If you run into any strange behaviour, as a matter of course you should try it in 1.4.X and compare to results.

    — answered November 25th 2010 by Paul Dowsett
    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.