【问题标题】:Titanium - creating a single column picker with data from jsonTitanium - 使用来自 json 的数据创建单列选择器
【发布时间】:2012-05-04 11:18:57
【问题描述】:

我正在尝试创建一个单列选择器,其数据源自 json 文档。现在我正在为 iOS 设备编写代码,但计划对 android 使用相同的代码。当我运行此代码时,选择器为空。任何帮助表示赞赏!

////
var win = Titanium.UI.createWindow({
    title:"Creating a Single-Column Picker",
    backgroundColor:"#FFF",
    exitOnClose:true
});

var url ="http://stats.catchitkansas.com/sunflower/cik/xml/app/app_master_schools.json"; //  rss     feed url

var json, object_name, locatie, i, row, title, val;

//A single column picker

var picker = Ti.UI.createPicker({
bottom:0,
});

var xhr = Ti.Network.createHTTPClient({ 

    onload: function(e) { // Ti.API.debug(this.responseText);

        json = JSON.parse(this.responseText); 
            for (i = 0; i < json.object_name.length; i++) { 

            locatie = json.object_name[i]; 

            picker.add(Ti.UI.createPickerRow({title: e.locatie.title}));
            }   
        //Now, we need add the pickerRow objects in the data array to the picker
        Ti.API.debug(this.responseText);
        alert('success');

        }, 
    }

);
picker.selectionIndicator = true;

xhr.open('GET',url);

xhr.send();

//This label contains text that will change with each picker change
var results = Titanium.UI.createLabel({
    text:"Select from the picker below",
    height:40,
    width:"auto",
    top:20  
});

//When using a picker, listen for the "change" event
picker.addEventListener("change", function(e){
    results.text = e.row.title + ": " + e.row.val; //Pull the title and val properties from the     selected pickerRow object (accessed via e.row)
});

win.add(picker);
win.add(results);

win.open();

【问题讨论】:

    标签: xml json titanium picker


    【解决方案1】:

    问题 #1

    您的数据馈送返回格式不正确的 JSON。

    改变这个

    "object_name": {

    "object_name":

    (通过http://jsonlint.com/运行JSON找到)


    问题 #2

    改变这个

    picker.add(Ti.UI.createPickerRow({title: e.locatie.title}));

    picker.add(Ti.UI.createPickerRow({title: locatie.title}));

    locatie 对象不是事件变量e 的一部分,它是一个独立变量。


    问题 #3

    在 HttpClient 请求完成之前添加选择器。

    您应该将win.add(picker); 移动到onload 回调内的for 循环之后。

    var xhr = Ti.Network.createHTTPClient({ 
        onload: function(e) { 
            json = JSON.parse(this.responseText); 
    
            for (i = 0; i < json.object_name.length; i++) { 
                locatie = json.object_name[i];
    
                picker.add(Ti.UI.createPickerRow({title: locatie.title}));
            }   
    
            win.add(picker);
            alert('success');
        } 
    });
    

    【讨论】:

    • 我假设选择器仍然没有显示任何内容。您能否更新原始来源以显示正确的提要?
    • 来源已更新。并且选择器仍然没有显示信息。但是感谢您使用 JSON 指出正确的方向。一开始我并不确定。
    • 谢谢马克!你摇滚!像冠军一样工作。
    猜你喜欢
    • 2012-12-07
    • 2020-03-26
    • 1970-01-01
    • 2022-08-18
    • 1970-01-01
    • 2018-02-14
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多