Retrieve values to the Picker using Json

You must Login before you can answer or comment on any questions.

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);

};

— asked 11 months ago by Suresh Kumar S
1 Comment
  • Now I am able to retrieve data from the picker... But in the Picker, it displays only the last value added to the DB.... Please help... Here is my code..

    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 picker = Ti.UI.createPicker();

    var data = []; data[0]=Ti.UI.createPickerRow({title:''+ json[pos].product_name + '',custom_item:'b'});

    // turn on the selection indicator (off by default) picker.selectionIndicator = true;

    picker.add(data); currentWin.add(picker);

    }; }

    — commented 11 months ago by Suresh Kumar S

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).

— answered 11 months ago by Jason Priebe
answer permalink
1 Comment
  • Now I am able to retrieve data from the picker... But in the Picker, it displays only the last value added to the DB....It does not display all the values in the column of the DB.... Please help... Here is my code..

    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 &lt; json.length; pos++){ var picker = Ti.UI.createPicker(); var data = []; data[0]=Ti.UI.createPickerRow({title:''+ json[pos].product_name + '',custom_item:'b'}); // turn on the selection indicator (off by default) picker.selectionIndicator = true; picker.add(data); currentWin.add(picker); }; }

    — commented 11 months ago by Suresh Kumar S

Your Answer

Think you can help? Login to answer this question!