Created
June 10, 2011 01:35
-
-
Save pec1985/1018107 to your computer and use it in GitHub Desktop.
delete a row in a table view in android
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:'#ccc' | |
}); | |
// create the table view | |
var tableView = Ti.UI.createTableView({}); | |
// create an empty array | |
var data = []; | |
// this is the alert message + deleting the row from the array | |
function confirmDel(rowId,rowName) { | |
var msgTitle = "Delete Row"; | |
var msgText = "Are you sure you want to delete the '"+rowName+"'?"; | |
var statusAlert = Ti.UI.createAlertDialog({title:msgTitle,message:msgText,buttonNames: ['Cancel','Ok']}); | |
statusAlert.show(); | |
statusAlert.addEventListener('click',function(e){ | |
if (e.index === 0){statusAlert.hide();} | |
if (e.index === 1){ | |
for(var i = 0; i< data.length; i++){ | |
if(data[i].rowId === rowId){ | |
data.splice(i,1); | |
break; | |
} | |
} | |
refresh(); | |
} | |
}); | |
} | |
// this is the tap-and-hold | |
function tapAndHold(a,b){ | |
a.addEventListener('touchstart', function(){ | |
var timer = setTimeout(function() { | |
confirmDel(b.rowId,b.title); | |
}, 500); | |
a.addEventListener('touchend', function(){ | |
clearTimeout(timer); | |
}); | |
tableView.addEventListener('scroll', function(){ | |
clearTimeout(timer); | |
}); | |
}); | |
} | |
// create all the table view rows | |
for (var i=0; i < 70; i++) { | |
var row = Ti.UI.createTableViewRow({ | |
title:'row #'+(i+1), | |
rowId: i, | |
color:'black', | |
className:'my-row', | |
backgroundColor:'white' | |
}); | |
// this is a work around for a "touchstart" bug on the rows for android | |
var view = Ti.UI.createView({top:0,bottom:0,left:0,right:0, backgroundColor:'transparent'}); | |
view.add(Ti.UI.createLabel({text:row.title, fonft:{fontSize:20,fontWeight:'bold'}})); | |
row.add(view); | |
// add the tap-and-hold listener to the row | |
tapAndHold(view,row); | |
// add the row to the array | |
data.push(row); | |
}; | |
// create the refresh function | |
function refresh(){ | |
tableView.data = data; | |
}; | |
// call the refresh function | |
refresh(); | |
// add the table view to the window | |
win.add(tableView); | |
// open the window | |
win.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment