选择器更改事件在Titanium中首次未触发 [英] picker change event not firing for first time in Titanium

查看:130
本文介绍了选择器更改事件在Titanium中首次未触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Tiatnium应用程序中使用选择器。



选择器数据正在从json响应中加载。我首先在临时数组中存储API数据,然后将数据添加到选择器。



问题是当我第一次单击选择器元素时,更改事件不会被触发。
第一次点击完成后,如果我点击任何元素,它按预期工作。



这是小代码片段

$ b对于(var i = sorted.length - 1; i> = 0; i--)的
$ b

  {
pickerData [i] = Ti .UI.createPickerRow({
title:sorted [i],

});
Ti.API.info('From sorted'+ i + sorted [i]);
}
$ .picker.add(pickerData);
$ .picker.addEventListener('change',function(e){
countRow = 0;
data.length = 0;
showfilterData(e.row.title,jsonResponse );
});

那么问题是什么。



有人可以解释吗?

解决方案

这是很长时间来回答这个问题,但是我已经找到了一种新的方式来完成这项任务


  1. 在js文件中创建选择器在更改事件中运行良好。

  2. 如果您已经在XML文件中创建了选择器,并且您在js文件中添加选择器行,那么您需要调用以下方法两次。这很奇怪,但它整齐地工作。

index.xml

 < Alloy> 
< Window>
< Picker id =PICKERwidth = '70%'height =Ti.UI.SIZEonChange =changeValue>< / Picker>
< Window>
< / Alloy>

index.js

 (函数setPickerValues(){
$ .PICKER.add(rows)// rows是Ti.UI.PickerRows的数组

//调用这两行以使更改事件工作
$ .PICKER.setSelectedRow(0,1); //这将将选择器行设置为索引1
$ .PICKER.setSelectedRow(0,0); //现在有必要再次设置索引号为0的行,使其正常工作
})();

函数changeValue(e){
Ti.API.info('Current Row ='+ e.rowIndex);
}

我知道这样很奇怪,但是它的工作相当不错,比js文件中创建整个选择器更少的代码行。


I am using picker in my Tiatnium Application.

Picker data is loading from json response.I first store API data in Temporary array and then adding data to picker.

The problem is when I click picker elements for the first time the change event doesn't get fired. After first click is done then if I click on any element it works as expected.

Here is the small code snippet of it

for (var i = sorted.length - 1; i >= 0; i--) {
            pickerData[i] = Ti.UI.createPickerRow({
                title : sorted[i],

        });
            Ti.API.info('From sorted ' + i + sorted[i]);
        }
$.picker.add(pickerData);
$.picker.addEventListener('change', function(e) {
            countRow = 0;
            data.length = 0;
            showfilterData(e.row.title, jsonResponse);
        });

So what is the problem.

Can anyone explain?

解决方案

This is long time to answer this question, but I have found a new way to do this task using Alloy XML as well.

  1. Creating picker in js file works well in change event.
  2. If you have created picker in XML file and you are adding picker rows in js file, then you need to call the below method twice. It's little odd but it works neatly.

index.xml

<Alloy>
<Window>
    <Picker id="PICKER" width='70%' height="Ti.UI.SIZE" onChange="changeValue"></Picker>
<Window>
</Alloy>

index.js

(function setPickerValues() {
   $.PICKER.add(rows)   // rows is an array of Ti.UI.PickerRows

   // call these two lines to make the change event work
   $.PICKER.setSelectedRow(0, 1);   // this will set the picker row to the row at index 1
   $.PICKER.setSelectedRow(0, 0);   // now it is necessary to set row at index 0 again to make it work.
})();

function changeValue(e) {
   Ti.API.info('Current Row = ' + e.rowIndex);
}

I know this way is little odd, but yes it works pretty well and takes less lines of code than creating whole picker in js file.

这篇关于选择器更改事件在Titanium中首次未触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆