【问题标题】:want to specify each row of tableView from data from Appcelerator cloud想要从 Appcelerator 云中的数据中指定 tableView 的每一行
【发布时间】:2014-07-04 09:03:42
【问题描述】:

我正在开发一个带有钛/appcelerator 云服务的 PC 安卓应用程序。

我想要 facebook 的新闻提要页面,其中用户的帖子列表下来。

我已经设法使用 tableView 做到这一点,每行显示来自服务器的每个帖子。

问题是我无法获取单个用户的照片,并且(每个帖子都有按钮)如果我单击此按钮,它不会给我该特定用户的用户信息,而是该用户的用户信息第一次发帖。

例如,如果 user1、user2、user3 各发布一个, 列表页面分别显示 post1、post2、post3。 如果我单击 post2 的按钮(这是发送消息按钮),它会尝试将其发送给 user1 而不是 user2。

这是我的代码。

过去 2 天被卡住了,, 谢谢!

profileimage 是照片的变量 购物是消息发送按钮的变量

win.addEventListener('open', function () {
Cloud.Posts.query({
    page: 1,
    per_page: 20,
    order: "-created_at"
}, function (e) {
    if (e.success) {
        status.hide();

      var tbl_data = [];   
        for (var i = 0; i < e.posts.length; i++) {
            var post = e.posts[i];

        -- there are some code here ---

            var profileimage = Ti.UI.createView({
                backgroundImage: post.user.photo.urls.medium_500,
                backgroundColor: '#fffff',
                width: 90,
                height: 90,
                borderRadius: 22,
                top:20, left: 10
            });

            var shopping = Ti.UI.createView({
                backgroundImage: 'shopping.png',
                width: 35,
                height: 35,
                top: 70,
                right: 10
            });

            shopping.addEventListener('click', function(){
                //alert(post);
                var tradewin,tradeinc;
                    tradeinc = require('trade');
                    tradewin = new tradeinc.trade1(post);
                    tradewin.open();
            });

【问题讨论】:

    标签: javascript android titanium tableview appcelerator


    【解决方案1】:

    您的问题似乎源于在for 循环内为所有shopping 视图添加点击事件侦听器。当shopping click 函数由于用户操作而被实际调用时,它会访问post 的最新值,这将始终是post 在for 循环结束时的值:e.posts[e.post.length - 1]。我已修改您的代码以缓解此问题:

    win.addEventListener('open', function () {
        Cloud.Posts.query({
            page: 1,
            per_page: 20,
            order: "-created_at"
        }, function (e) {
            if (e.success) {
                status.hide();
    
                var tbl_data = [];   
                for (var i = 0; i < e.posts.length; i++) {
                    var post = e.posts[i];
    
                -- there are some code here ---
    
                    var profileimage = Ti.UI.createView({
                        backgroundImage: post.user.photo.urls.medium_500,
                        backgroundColor: '#fffff',
                        width: 90,
                        height: 90,
                        borderRadius: 22,
                        top:20, left: 10
                    });
    
                    var shopping = Ti.UI.createView({
                        backgroundImage: 'shopping.png',
                        width: 35,
                        height: 35,
                        top: 70,
                        right: 10
                    });
    
                    // This wrapper function returns a callback function
                    // for your click event that also maintains the correct
                    // value of `post`
                    function getShoppingClickCallback(associatedPost) {
                        return function () {
                            var tradewin, tradeinc;
    
                            tradeinc = require('trade');
                            tradewin = new tradeinc.trade1(associatedPost);
                            tradewin.open();
                        };
                    }
    
                    // This line calls the `getShoppingClickCallback` function and
                    // passes the associated `post` object. The returned value of
                    // `getShoppingClickCallback` is a callback function.
                    shopping.addEventListener('click', getShoppingClickCallback(post));
    

    在for 循环中添加事件侦听器时使用类似getShoppingClickCallback 的辅助函数是一种非常有用的模式。我建议在您的项目中尝试一下,看看您的问题是否仍然存在。我希望不会! :)

    【讨论】:

    • 不客气!如果您不介意将我的答案标记为正确,我们将不胜感激!
    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多