-
-
Save vindriaoc/939063 to your computer and use it in GitHub Desktop.
TableView With DB and tap and hold
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var win = Ti.UI.createWindow({ | |
backgroundColor: 'white' | |
}); | |
var tableView = Ti.UI.createTableView(); | |
var setupDB = function() { | |
var db = Ti.Database.open('mydb.db'); | |
db.execute('CREATE TABLE IF NOT EXISTS DATABASETEST (ID INTEGER, NAME TEXT)'); | |
db.execute('DELETE FROM DATABASETEST'); | |
db.execute('INSERT INTO DATABASETEST (ID, NAME) VALUES(?,?)',1,'Name 1'); | |
db.execute('INSERT INTO DATABASETEST (ID, NAME) VALUES(?,?)',2,'Name 2'); | |
db.execute('INSERT INTO DATABASETEST (ID, NAME) VALUES(?,?)',3,'Name 3'); | |
db.execute('INSERT INTO DATABASETEST (ID, NAME) VALUES(?,?)',4,'Name 4'); | |
db.close(); | |
}; | |
setupDB(); | |
var tapAndHold = function(e){ | |
e.addEventListener('touchstart', function(e){ | |
timer = setTimeout(function() { | |
alert(e.source.rowId+":"+e.source.rowName); | |
// confirmDel(e.source.rowId,e.source.rowName); | |
}, 500); | |
}); | |
e.addEventListener('touchend', function(e){ | |
clearTimeout(timer); | |
}); | |
}; | |
var getItemsFromDB = function() { | |
var db = Ti.Database.open('mydb.db'); | |
var rows = db.execute('SELECT * FROM DATABASETEST'); | |
var data = []; | |
while (rows.isValidRow()) | |
{ | |
var name = rows.fieldByName('name'); | |
var id = rows.fieldByName('id'); | |
var row = Ti.UI.createTableViewRow({ | |
title: name, | |
color:'black', | |
touchEnabled:false | |
}); | |
var label = Ti.UI.createLabel({ | |
text: name, | |
height:'auto', | |
width:'auto', | |
left:10, | |
textAlign:'center' | |
}); | |
var rowView = Titanium.UI.createView({ | |
// backgroundColor:'transparent', | |
height:52.5, | |
rowId: id, | |
rowName: name | |
}); | |
tapAndHold(rowView); | |
row.add(rowView); | |
row.add(label); | |
data.push(row); | |
rows.next(); | |
} | |
rows.close(); | |
db.close(); | |
return data; | |
}; | |
tableView.setData( getItemsFromDB() ); | |
win.add(tableView); | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment