Hi I want to retrieve values to the picker using json...
I have two column(Name) and (Quantity) in the PHPMyAdmin DB.... Trying to retrieve (Quantity)values to the Picker..
Here is my code... Please help....
var currentWin = Ti.UI.currentWindow;
var sendit = Ti.Network.createHTTPClient();
sendit.open('GET', 'http://localhost/mobileapp/productread.php');
sendit.send();
sendit.onload = function(){
var json = JSON.parse(this.responseText);
var json = json.mobile_product;
var dataArray = [];
var pos;
for( pos=0; pos < json.length; pos++){
var data = [{
title:'' + json[pos].product_name + ''
}]
// set the array to the tableView
picker.add(data);
};
var picker = Ti.UI.createPicker({
height:"20%",
width:"70%"
}); currentWin.add(picker);
};
2 Answers
Accepted Answer
Your code is very difficult to read. Please learn to use the Markdown syntax to format your code (use three "~" characters before and three "~" characters after). Click on the various "Markdown Syntax" links above the textfield to learn more.
Anyway, your problem is that you're creating a new picker each time through your loop. Instead, you should create the picker just once, build an array of rows, and add the rows just once at the end:
var picker = Ti.UI.createPicker(); // turn on the selection indicator (off by default) picker.selectionIndicator = true; var data = []; var pos; for (pos=0; pos < json.length; pos++) { data.push (Ti.UI.createPickerRow({title:''+ json[pos].product_name + '',custom_item:'b'})); } picker.add(data); currentWin.add(picker);
What exactly is the problem? You don't tell us what happens.
Without knowing the exact symptoms you're seeing, I would recommend that you create the picker before you call sendit.send().
If you don't do that, you have a potential race condition where the HTTP results come in before you've created the picker (probably not very likely, but the way your code is structured, it is at least possible).
Your Answer
Think you can help? Login to answer this question!