【问题标题】:Appcelerator Titanium: Using eventListener & fireEvent on Database Array on ViewAppcelerator Titanium:在视图上的数据库数组上使用 eventListener 和 fireEvent
【发布时间】:2015-05-05 10:39:59
【问题描述】:

我是钛新手。一段时间以来,我一直在这方面遇到一些问题。帮助将不胜感激。

我有一个循环遍历数据库以检索图像和标签的数组循环。这些都存储在视图中。我正在向视图添加一个事件侦听器,然后向侦听器添加触发事件以在新窗口中打开选定的图像,该视频的 id 作为在新窗口中播放选定视频的参考点。希望我的代码更有意义。

问题是偶数监听器只会返回数据库中最后一行的值,而不是用户点击的视频图像的信息

function getChannelVideos(){
    // create an empty data array
    var video = [];

    // create the httpRequest 
    var xhr = Titanium.Network.createHTTPClient(); 

    xhr.open('GET','getVideoFeed.php?chid='+channelView.channelId); 


    // this method will be called when the request is complete
    xhr.onload = function() 
    { 

        // parse json coming from the server
        var json = JSON.parse(this.responseText);

        // if Channel Videos are returned
        if(json.channelVideos)
        {
            for (var i = 0; i < json.channelVideos.length; i++){

                var video = Ti.UI.createView({  // creates video view
                    width:'60%', // sets height
                    backgroundColor: 'transparent',
                });

                var videoThumb = Ti.UI.createImageView({  // creates thumb
                    image:json.channelVideos[i].vThumb, 
                    width:'100%', // sets height
                    top:150 + i*200, // positions from top
                    backgroundColor: '#000'
                });
                video.add(videoThumb);  // adds thumb to video view

                var videoTitle = Ti.UI.createLabel({  // creates label
                    text:json.channelVideos[i].vTitle,
                    font:{
                        fontSize:12
                    },
                    color:'#fff',
                    backgroundColor:'#000',
                    textAlign:'center',
                    width:'100%',
                    height:20,
                    top:140 + i*200, // positions from top
                });
                video.add(videoTitle);  // adds video title to video view

                var videoSpeaker = Ti.UI.createLabel({  // creates Label
                    text:json.channelVideos[i].vSpeaker,
                    font:{
                        fontSize:12
                    },
                    color:'#fff',
                    backgroundColor:'#000',
                    textAlign:'center',
                    width:'100%',
                    height:20,
                    top:295 + i*200, // positions from top
                });
                video.add(videoSpeaker);  // adds speaker name to video view

                var videoPlay = Ti.UI.createImageView({
                    image:'/images/light_play.png',
                    top:215 + i*200, // positions from top
                });

                video.add(videoPlay);  // adds playbutton to videoview

                chContentAreaView.add(video);  // adds video view to scrollview 

                var vId=json.channelVideos[i].vId;

                video.vId = vId;  // Here vId is custom property of video

                video.addEventListener('click', function(e){
                    alert(e.source.vId);
                    Ti.App.fireEvent('videoPlay',{
                        videoId:e.source.vId                        
                    });
                });

            }

        }
    };

    // this method will be called if there is an error 
    xhr.onerror = function(){
        alert(this.error + ': ' + this.statusText);
        return false;
    };

    // open the httpRequest 
    xhr.setRequestHeader("contentType","application/json; charset=utf-8");
    xhr.send();
}

【问题讨论】:

  • 你使用的是哪个sdk?
  • 嗨,斯旺南德。我正在使用 Titanium SDK 3.5.1GA

标签: arrays events for-loop onclicklistener appcelerator


【解决方案1】:

尝试一些事情:

  1. 直接在视频属性中给video.vId = json.channelVideos[i].vId,比如:

    var video = Ti.UI.createView({ // 创建视频视图 width:'60%', // 设置高度 背景颜色:'透明', vId : json.channelVideos[i].vId });

  2. videoThumbvideoTitlevideoSpeakervideoPlay等的其他视图和标签中使用属性touchEnabled : false

我认为这样做一切都会好起来的。

【讨论】:

  • 感谢您的建议。试过了,还是不行:我当前的代码是这样的: video.addEventListener('click', function(e){ alert(e.source.vId); Ti.App.fireEvent('videoPlay',{ videoId :e.source.vId }); });
  • 我的视图看起来像这样: var video = Ti.UI.createView({ // 创建视频视图 width:'60%', // 设置高度 backgroundColor: 'transparent', vId : json .channelVideos[i].vId });
  • 所有其他图像、标签等都已 touchEnabled: false 正如您所建议的那样
猜你喜欢
  • 2012-02-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-05
  • 2023-03-03
  • 1970-01-01
  • 2016-05-09
  • 1970-01-01
相关资源
最近更新 更多