Hi guys,
Onto the last piece of my latest app, and i'm having a slight issue I can't seem to fix.
Basically, I want the user to click the 'drop' button and for the app to pop an alert to confirm it has been dropped, after the SQL has run on the database.
The issue i'm having is that after the first deletion, which seems to work OK, it the fires twice the next time you tap it, 4 times on the 3rd time, etc etc
Any ideas what I might have done wrong?
Using iOS and Titanium 3.2.0
var win = Titanium.UI.currentWindow; var selectedlanguage = Ti.App.Properties.getString('langSelect'); // when you have set your labels with words //front.setBackgroundImage(front.toImage()); //label1.hide(); // just before you are ready to add the next word //label1.show(); //front.setBackgroundImage(undefined); // resets the background var masterView = Ti.UI.createView({ backgroundColor: '#FFF', top: 165, width: 300, height: 140, opacity: 0.7 }); var label1 = Ti.UI.createLabel({ //text: verb_german, text: '', textAlign: 'center', color: '#000', font: { fontSize: 30 }, top: 50 }); var label2 = Ti.UI.createLabel({ //text: verb_english, text:'', textAlign: 'center', color: '#000', font: { fontSize: 30 }, top: 50 }); var dropButton = Ti.UI.createButton({ width: 120, height: 41, right: 15, bottom: 15, title: 'drop word', backgroundColor: '#fd0100', color: '#FFF', font: { fontSize: 15 }, opacity: 1.0 }); win.add(dropButton); function loadWords() { // clear words first label1.text = ''; label2.text = ''; // get a random pair of words var db = Ti.Database.open('germanV5'); var rows = db.execute('SELECT * FROM Words WHERE word_dropped = 0 ORDER BY RANDOM() LIMIT 1;'); var x = 0; while (rows.isValidRow()) { if (selectedlanguage == 'en') { var word_1 = rows.fieldByName('word_english'); var word_2 = rows.fieldByName('word_german'); } else if (selectedlanguage == 'de') { var word_2 = rows.fieldByName('word_english'); var word_1 = rows.fieldByName('word_german'); } var word_id = rows.fieldByName('word_id'); rows.next(); } // close database rows.close(); var state = true; win.add(masterView); var front = Ti.UI.createView({ backgroundColor: '#FFF', top: 0, left: 0, width: 300, height: 140, opacity: 1.0, touchEnabled: false }); label1.text = word_1; front.add(label1); masterView.add(front); var back = Titanium.UI.createView({ backgroundColor: '#FFF', top: 0, left: 0, width: 300, height: 140, opacity: 1.0, touchEnabled: false }); label2.text = word_2; back.add(label2); masterView.addEventListener('click', function (e) { switch (state) { case true: Ti.API.info('true'); masterView.animate({ view: back, transition: Ti.UI.iPhone.AnimationStyle.FLIP_FROM_LEFT }); break; case false: Ti.API.info('false'); masterView.animate({ view: front, transition: Ti.UI.iPhone.AnimationStyle.FLIP_FROM_RIGHT }); break; } state = !state; }); // event listener for the drop button dropButton.addEventListener('click', function (e) { // update the DB to tell it the word has been dropped // pop an alert to notify the user the word has been dropped var alertDialog = Titanium.UI.createAlertDialog({ title: 'Word Dropped', message: 'This word has been dropped!' & word_id, buttonNames: ['OK'] }); // show the message alertDialog.show(); // load in a new word loadWords(); }); } // fire the function and load our words into play loadWords(); function removeWords() { //window.remove(masterView); } var tapLabel = Ti.UI.createLabel({ width: 200, top: 133, text: 'tap to flip', textAlign: 'center', color: '#FFF', font: { fontSize: 15 } }); var swipeLabel = Ti.UI.createLabel({ width: 320, top: 325, text: 'swipe for next word', textAlign: 'center', color: '#FFF', font: { fontSize: 15 } }); win.add(tapLabel); win.add(swipeLabel); var grammarButton = Ti.UI.createButton({ width: 120, height: 41, left: 15, bottom: 15, title: 'verb tables', backgroundColor: '#ffff01', color: '#000', font: { fontSize: 15 }, opacity: 1.0 }); win.add(grammarButton); swipeLabel.addEventListener('swipe', function (e) { // reload the new word loadWords(); }); masterView.addEventListener('swipe', function (e) { // reload the new word loadWords(); }); grammarButton.addEventListener('click',function(e){ var newWin = Titanium.UI.createWindow({ url:'verb_table.js', backgroundImage:'/images/background_random.jpg', backgroundColor:'#FFF', barColor:'#000', translucent: true, color:'#FFF', navTintColor:'#FFF', titleControl: Ti.UI.createLabel({ text: 'Verb Table', color: '#FFF' }), statusBarStyle:Titanium.UI.iPhone.StatusBar.LIGHT_CONTENT, backButtonTitle:'' }); newWin.nav = win.nav; win.nav.openWindow(newWin, {animated:true}); });Also, another issue I've noticed, I need to capture the ID number which will perform the update on the SQL and remove the correct word.
Code snippet below, you'd expect the alert to say 'This word has been dropped! 99' but instead all it returns is Zero, not even the words.
message: 'This word has been dropped! ' & word_id,
Many thanks
Simon