Picker only showing one value from database
My friends,
I have a picker pulling from a database but it only shows the first entry. I have tried various methods to pull all entries, but constantly get errors like missing semi-colon when I try to add a while loop or something to the pickerValues array even when I'm pretty sure there shouldn't be a semi-colon. The simple code is this:
//Get data for style picker
var rows = db.execute('SELECT * FROM style');
// picker initialization
var picker = Titanium.UI.createPicker({top:0});
picker.selectionIndicator=true;
var pickerValues = [
Titanium.UI.createPickerRow({title:rows.fieldByName('styleName'),id:rows.fieldByName('styleID')})
];
picker.add(pickerValues);
pickerView.add(picker);
I'm used to PHP and I think I keep trying to make calls that Titanium doesn't like. I am slowly getting used to the way it functions.
Thanks in advance,
Eric
2 Answers
-
Accepted Answer
I think the problem is how you get the database rows.
try like this:
pickerValues = []; while (rows.isValidRow()) { pickerValues.push( Titanium.UI.createPickerRow({title:rows.fieldByName('styleName'),id:rows.fieldByName('styleID')}) ) } picker.add(pickerValues);
//code untested
-
Thanks for the quick reply Tamas. Sorry I took so long to get back with you. I was on the road for 3 hours.
Your code pointed me in the right direction. I had tried something similar only to get the missing semi-colon error. I got the error when adding yours, but I added a semi-colon to end of your pickerValues.push statement and the error when away but the window was completely blank. I added rows.next(); and rows.close(); to the end and voilá. It worked like it should. Below is the final code that worked for anyone needing something similar.Thanks again, Tamas
while (rows.isValidRow()){ pickerValues.push( Titanium.UI.createPickerRow({title:rows.fieldByName('styleName'),id:rows.fieldByName('styleID'),value:rows.fieldByName('styleID')}) ); rows.next(); } rows.close();