[英]picker change event not firing for first time in Titanium
I am using picker in my Tiatnium Application. 我在Tiatnium应用程序中使用选择器。
Picker data is loading from json response.I first store API data in Temporary array and then adding data to picker. Picker数据是从json响应加载的。我首先将API数据存储在Temporary数组中,然后将数据添加到Picker中。
The problem is when I click picker elements for the first time the change event doesn't get fired. 问题是,当我第一次单击选择器元素时,不会触发change事件。 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? 谁能解释?
The following example works fine: 以下示例可以正常工作:
index.xml INDEX.XML
<Alloy>
<Window id="mainWindow">
</Window>
</Alloy>
index.js index.js
var picker = Titanium.UI.createPicker();
var data = [];
data[0]=Ti.UI.createPickerRow({title:'Bananas'});
data[1]=Ti.UI.createPickerRow({title:'Strawberries'});
data[2]=Ti.UI.createPickerRow({title:'Mangos'});
data[3]=Ti.UI.createPickerRow({title:'Grapes'});
picker.add(data);
picker.addEventListener('change', function(e) {
console.log(e.row.title);
});
$.mainWindow.add(picker);
$.mainWindow.open();
Every time (also the first one) that I click on a row of the picker I get the proper log on the console. 每当我(也是第一个)单击选择器的行时,都会在控制台上获得正确的日志。 Tested on Titanium 4.1.0, iOS 8.4 在Titanium 4.1.0,iOS 8.4上测试
The picker works fine. 选择器工作正常。 I guess there is something else wrong in your code (something you didn't show us). 我猜您的代码中还有其他问题(您没有向我们展示)。
Let me know if this example doesn't work for you 让我知道这个例子是否不适合您
This is the first time I am giving answer to my own question. 这是我第一次回答自己的问题。 For all those who have problem in getting first click of picker this may help you. 对于所有在选择器的首次点击时遇到问题的人,这可能会对您有所帮助。
Here is the code in which you might face issue. 这是您可能会遇到的代码。
<Alloy>
<Window id="winpast" class="container" title="Past issues" onOpen="openpastIssues">
<View id="view2" width="Ti.UI.FILL" height="Ti.UI.FILL" backgroundColor="#A9F5A9" >
<View id="viewcheck1" >
<Label id="title" width="Ti.UI.SIZE" text="Past Issues" height="Ti.UI.SIZE" textAlign="Ti.UI.TEXT_ALIGNMENT_CENTER"></Label>
<ImageView id="menuImg" image="/images/menu.png" onClick="showsideBar" left="5"></ImageView>
<Picker id="picker" selectionIndicator="true" height="Ti.UI.SIZE" width="Ti.UI.SIZE" right="10">
</Picker>
</View>
</View>
</Window>
js file js文件
for (var i = sorted.length - 1; i >= 0; i--) {
pickerData[i] = Ti.UI.createPickerRow({
title : sorted[i],
});
$.picker.add(pickerData);//picker is added in xml file and data is added now
picker.addEventListener('change', function(e) {
alert(e.row.title);
});
$.winpast.open();
The above snippets may have issue with picker first click. 上面的摘录可能与选择器的首次单击有关。
Now here is the correct way that I implemented to remove this issue with the advice of @Riccardo Bucco 现在这是我在@Riccardo Bucco的建议下实现的解决此问题的正确方法
I just gave a trial using his suggestion and found out this. 我只是利用他的建议进行了试验,并发现了这一点。
Here is proper code. 这是正确的代码。
<Alloy>
<Window id="winpast" class="container" title="Past issues" onOpen="openpastIssues">
<View id="view2" width="Ti.UI.FILL" height="Ti.UI.FILL" backgroundColor="#A9F5A9" >
<View id="viewcheck1" >
<Label id="title" width="Ti.UI.SIZE" text="Past Issues" height="Ti.UI.SIZE" textAlign="Ti.UI.TEXT_ALIGNMENT_CENTER"></Label>
<ImageView id="menuImg" image="/images/menu.png" onClick="showsideBar" left="5"></ImageView>
</View>
</View>
</Window>
js file js文件
var picker = Ti.UI.createPicker({
right:10
});
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);//we add data here and picker is not added yet
$.viewcheck1.add(picker);//after adding whole data we are adding picker in the view so it resolves the first click issue.
$.winpast.open();
The second way works fine for me and there is no first click issue with picker.If any thing in my answer is not properly mentioned then please correct me. 第二种方法对我来说很好,选择器没有第一次点击问题。如果我的答案中没有任何提及,请纠正我。
Hope it helps. 希望能帮助到你。
This is long time to answer this question, but I have found a new way to do this task using Alloy XML as well. 这是回答这个问题的时间很长的时间,但是我也找到了一种使用Alloy XML来完成此任务的新方法。
index.xml INDEX.XML
<Alloy>
<Window>
<Picker id="PICKER" width='70%' height="Ti.UI.SIZE" onChange="changeValue"></Picker>
<Window>
</Alloy>
index.js 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. 我知道这种方式有点奇怪,但是是的,与在js文件中创建整个选择器相比,它可以很好地工作并且花费更少的代码行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.