I have a JSON defined in the code with and image, a title, a date and a url. This will be used to populate the list view. On the click event of any list item, I need to navigate to a different controller and render the view in it having the videoplayer that plays the video according to the clicked list item's url and i want to display the date and title too. I am not understanding how to code this in the itemClick event. Please help!
This is my .js file for DashboardController.
Alloy.createController('VideoPlayerController',videoInfo[i]).getView();
var sections = [];
//JSON to populate the listview
var videoInfo = [{
pic : "/Images/playButton.png",
info : "This in the the title for the first video.",
date : "03/07/2017",
url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}, {
pic : "/Images/playButton.png",
info : "This in the the title for the second video.",
date : "03/07/2017",
url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}, {
pic : "/Images/playButton.png",
info : "This in the the title for the third video.",
date : "03/07/2017",
url : "https://youtu.be/zkOSR0ZRb9khttps://youtu.be/zkOSR0ZRb9k"
}];
function populateListView() {
Ti.API.trace("[DashboardController.js >> populateListView() >>]");
if (!_.isEmpty(videoInfo)) {
for (var i = 0; i < videoInfo.length; i++) {
var item = {
template : "videoTemplate",
iconId : {
image : videoInfo[i].pic
},
titleId : {
text : videoInfo[i].info
},
dateId : {
text : videoInfo[i].date
},
urlId : {
text : videoInfo[i].url
},
properties : {
backgroundColor : "transparent"
}
};
sections.push(item);
}
$.listSection.setItems(sections);
}
}
populateListView();
$.lView.addEventListener('click',function(e){
var dataItem = $.listSection.getItemAt(e.itemIndex);
});
And the .xml file for DashboardController isthis :
<Alloy>
<View id="win2" class="container">
<View id = "v1" class ="view1" layout ="horizontal" >
<Button class="btnBack" ></Button>
<Label class = "label1">LIST VIEW EXAMPLE</Label>
</View>
<View class="view2">
<TextField id = "txtSearch"></TextField>
</View>
<ListView id = "lView" class = "list1" >
<Templates>
<ItemTemplate name="videoTemplate">
<View class = "viewTemplate" layout = "horizontal" >
<ImageView bindId="iconId" id="pic" />
<View class = "viewTitle" layout = "vertical" >
<Label bindId="titleId" id="info" />
<View class="viewDtUrl" layout="horizontal" >
<Label bindId="dateId" id="date" />
<Label bindId="urlId" id ="url" />
</View>
</View>
</View>
</ItemTemplate>
</Templates>
<ListSection id = "listSection">
</ListSection>
</ListView>
</View>
</Alloy>
This is the .xml file of the controller that will be rendering the video player (VideoPlayerController) is this:
<Alloy>
<View class="container">
<VideoPlayer class = "video"></VideoPlayer>
<View class="videoView">
<Label class="titleInfo"></Label>
<View class = "infoLabel">
<Label class="dateInfo"></Label>
<Label class="urlInfo"></Label>
</View>>
</View>
</View>
</Alloy>
You are very close. Here are a couple of items:
Alloy.createController('VideoPlayerController',videoInfo[i]).getView();
down into thelView
event listener.itemclick
, notclick
, so change that.videoInfo
array is serving as our data collection. Theitemclick
callback will be returned the index of the row clicked, and this will match the order of ourvideoInfo
array, so we can just pass it tocreateController
as:videoInfo[e.itemIndex]
createController
is returning a view with.getView()
, in our case, this probably aTiUIWindow
object, so we need to open that window with.open()
.This gives us:
Now in our
VideoPlayerController.js
, we'll have something like:From there, you can use the data passed to
args
to set up the rest of your window.