Hi All,
Today in this example I will show you how to use SQLite database with PhoneGap and jQuery Mobile Architecture.
From this example you will get to know how to override the default Android Back Button behaviours and also how
to use both tap and taphold event.
So lets begin, I have given the name of the application is 'MyContacts'. So first create an android project using phonegap and eclipse.
If you are unable to do this please go through Hello World! with PhoneGap, Android and Eclipse tutorial.
Now create three divs/pages in your index.html page. And also make sure whatever ID's you are using to select the DOM element or to bind some events with that
element has to be unique. The moment you introduce a duplicate id, you have an invalid document on your hands.
Now add the following JavaScript code inside a script tag in index.html page.
$(document).ready(function() {
document.addEventListener("deviceready", onDeviceReady, false);
var db = window.openDatabase("Database", "1.0", "MyContactsDB", 200000);
function onDeviceReady() {
//Populate the databse
db.transaction(populateDB, errorCB, successCB);
//Override the back button functionality
document.addEventListener('backbutton', onBack, false);
}
function onBack() {
//If the current page is index page then exit other wise navigate to index page
if ($.mobile.activePage.is('#index')) {
navigator.app.exitApp();
} else {
db.transaction(queryDB, errorCB);
}
}
function populateDB(tx) {
//Create the table
//tx.executeSql('DROP TABLE IF EXISTS MyContacts');
tx.executeSql('CREATE TABLE IF NOT EXISTS MyContacts (id INTEGER PRIMARY KEY AUTOINCREMENT, \
name TEXT NOT NULL, nickName TEXT, mobilePhoneNumber INT, \
workPhoneNumber INT, emailId TEXT, website TEXT, happyBirthDay TEXT)\
');
tx.executeSql('SELECT id, name, nickName FROM MyContacts ORDER BY name', [], querySuccess, errorCB);
}
function successCB() {
db.transaction(queryDB, errorCB);
}
function queryDB(tx) {
tx.executeSql('SELECT id, name, nickName FROM MyContacts ORDER BY name', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
$.mobile.showPageLoadingMsg(true);
var len = results.rows.length;
$("#userList").html('');
for (var i = 0; i < len; i++) {
var row = results.rows.item(i);
var htmlData = '
').listview('refresh');
$popup.popup();
$popup.popup('open');
$("#tapHoldCheck").val('true');
} else if (event.type === 'tap') {
if ($("#tapHoldCheck").val() == '') { //If the value of the text box is blank then only tap will work
db.transaction(function(tx) {
tx.executeSql("SELECT name, nickName, mobilePhoneNumber, workPhoneNumber, emailId, website, happyBirthDay FROM MyContacts WHERE id=?;", [liId], function(tx, results) {
var row = results.rows.item(0);
$.mobile.showPageLoadingMsg(true);
$.mobile.changePage($("#displayDataPage"), {
transition: "slide"
});
$("#nameHeader").html(row['name']);
$("#dataName").html(row['name']);
$("#dataNickName").html(row['nickName']);
$("#dataMobilePhoneNumber").html(row['mobilePhoneNumber']);
if (row['mobilePhoneNumber'] != '') {
$("#mpnCallSMS").html(
'
'
);
} else {
$("#wpnCallSMS").html('');
}
$("#dataEmailId").html('' + row['emailId'] + '');
$("#dataWebsite").html('' + row['website'] + '');
$("#dataHappyBirthDay").html(row['happyBirthDay']);
$('#dataList').trigger('create');
$('#dataList').listview('refresh');
$.mobile.hidePageLoadingMsg();
});
});
}
}
});
//Change the hidden field value when the popup is closed
$('#actionList-popup').bind({
popupafterclose: function(event, ui) {
$("#tapHoldCheck").val('');
}
});
$("#index [data-role='popup'] ul").on('click', 'li', function(event) {
var action_liId = this.id.split('&');
var action = action_liId[0];
var id = action_liId[1];
if (action == 'edit') {
db.transaction(function(tx) {
tx.executeSql("SELECT name, nickName, mobilePhoneNumber, workPhoneNumber, emailId, website, happyBirthDay FROM MyContacts WHERE id=?;", [id], function(tx, results) {
var row = results.rows.item(0);
$("#name").val(row['name']);
$("#nickName").val(row['nickName']);
$("#mobilePhoneNumber").val(row['mobilePhoneNumber']);
$("#workPhoneNumber").val(row['workPhoneNumber']);
$("#emailId").val(row['emailId']);
$("#website").val(row['website']);
$("#happyBirthDay").val(row['happyBirthDay']);
$("#id").val(id);
$("#addNewPageHeader").html('Edit');
$.mobile.changePage($("#addNewPage"), {
transition: "slide",
reverse: true
});
});
});
}
if (action == 'delete') {
navigator.notification.confirm(
'Are you sure?',
function(buttonIndex) {
onConfirm(buttonIndex, id);
},
'Delete Contact',
'Ok, Cancel'
);
}
});
function onConfirm(buttonIndex, id) {
if (buttonIndex === 1) { //Delete
db.transaction(function(tx) {
tx.executeSql("DELETE FROM MyContacts WHERE id=?", [id], queryDB, errorCB);
});
}
if (buttonIndex === 2) {
$.mobile.changePage($("#index"), {
transition: "slide"
});
}
}
});
While at the time of developing this application I have faced the problem of tap and taphold event.
The problem is that the tap event is fired after the taphold event once I lift my finger. So to resolve this problem I have used a
function popupafterclose and a hidden filed (id of the hidden field is 'tapHoldCheck').
The logic behind that is that, by default the value of the hidden field is blank, so when the taphold event is fired I am putting some value
on the hidden field and also at the time of closing the popup window again the value of hidden field becomes blank.
And finally your index.html page will look like following. My Contacts
No comments:
Post a Comment