Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-16455] Android: Event object on ListItem child's change doesn't match iOS

GitHub Issuen/a
TypeBug
PriorityHigh
StatusClosed
ResolutionInvalid
Resolution Date2020-02-19T13:48:53.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsparity
ReporterTim Poulsen
AssigneeRene Pot
Created2014-02-12T14:35:34.000+0000
Updated2020-02-19T13:48:53.000+0000

Description

Source: http://developer.appcelerator.com/question/162482/alloy-listview-with-switch-in-template The event object, in particular e.source does not include the same properties as on iOS making it impossible to create a cross-platform solution to the developer's question. In addition to the code I provided in that Q&A thread, I tried other solutions including this:
<Alloy>
	<Window title="List item templates">
		<ListView id="myList" defaultItemTemplate="myTemplate" top="20">
		    <Templates>
		        <ItemTemplate name="myTemplate" onChange="doSwitchMe">
		            <Switch bindId="checked" value="false" />
		            <Label bindId="title" left="40dp"/>
		        </ItemTemplate>
		    </Templates>
		    <ListSection id="mySection" headerTitle="A Section">
		    </ListSection>
		</ListView>
	</Window>
</Alloy> 
var items=[];
for(var i=0; i<4; i++){
    items.push({
        title: {text: 'Title one'}, 
        checked: {value: false, myID: 'switch'+i}
    });
};
$.mySection.setItems(items);

function doSwitchMe(e) {
    // result of next line is 'Switch undefined was clicked and is now on'
    alert('Switch ' + e.source.myID + ' was clicked and is now ' + ((e.source.value===true)? 'on' : 'off'));
}

$.index.open();
In all cases, e.source doesn't include the custom myID property.

Attachments

FileDateSize
Screen Shot 2014-02-12 at 9.07.13 AM.png2014-02-12T14:35:34.000+000097465
Screen Shot 2014-02-12 at 9.07.44 AM.png2014-02-12T14:35:34.000+000094639

Comments

  1. Hieu Pham 2014-02-13

    Yes, this is an architecture parity between Android and iOS. We solved this by introducing 'itemId' property that you can use instead:
       for(var i=0; i<4; i++){
           items.push({
               properties: {itemId: 'switch ' +i},
               title: {text: 'Title one'}, 
               checked: {value: false}
           });
       };
       ...
       function doSwitchMe(e) {
           // result of next line is 'Switch undefined was clicked and is now on'
           alert('Switch ' + e.itemId + ' was clicked and is now ' + ((e.source.value===true)? 'on' : 'off'));
       }
       
  2. Tim Poulsen 2014-02-13

    Defining a custom property on each list item is an acceptable workaround. But since the e.source.value property is valid on both platforms, it really seems as though the other properties of that child item should be accessible on the e.source object. I've updated the Q&A thread with the workaround. Thanks Hieu.

JSON Source