favorites (database) help
Hello Titanium friends,
This is my first attempt at Titanium and I have a simple app nearly completed. Have been able to find examples and figure out most of what I need (this forum is great) - except for the function of adding (by swipe or button) a row from an existing menu tableview and moving it to a favorites tab window, thus creating a tableview of favorites. Kitchen sink has an example of the delete function of the tableview once the favorites are in there but i cannot seem to figure out how to add to favorite window.
frustrated.
Any help or sample code would be most appreciated ;)
2 Answers
-
Hi there.
I have just created this and its working. Hope its not bad practice :)
Im working on getting the favourites window to refresh onload so it reloads the database. will pass it on once I have it working.
// Top of the page var win = Titanium.UI.currentWindow; var db = Titanium.Database.open('fav'); db.execute('CREATE TABLE IF NOT EXISTS fav (bid INTEGER PRIMARY KEY, name TEXT, distance INTEGER)'); // Add EventListener to insert fav details AddFavorites.addEventListener('click',function(){ db.execute('INSERT INTO fav (bid,name,distance) VALUES(?,?,?)',bid,name,miles); // bid,name,miles are my variables });
Then the Fav tab content
// This is Favorites Tab section var db = Titanium.Database.open('fav'); db.execute('CREATE TABLE IF NOT EXISTS fav (bid INTEGER PRIMARY KEY, name TEXT, distance INTEGER)'); var data = []; var rows = db.execute('SELECT * FROM fav'); while (rows.isValidRow()) { var businessName = Ti.UI.createLabel({ text: rows.fieldByName('name'), // other elements removed for speed. }); var businessMiles = Ti.UI.createLabel({ text:'Distance: '+ rows.fieldByName('distance')+' miles', // other elements removed for speed. }); var row = Ti.UI.createTableViewRow({ path:'business_details_template.js', bid: rows.fieldByName('bid') }); row.add(businessName); row.add(businessMiles); data.push(row); rows.next(); } rows.close(); // tableView = Titanium.UI. CreateTableView code here //win.add(tableView) etc. // Adding the delete function. tableView.addEventListener('delete',function(e) { db.execute("DELETE FROM fav WHERE bid = ?", e.rowData.bid); });
-
Thanks for an extensive answer, but personally I can't get this to work. Would love to see some more extensive code or explaination…