Reputation: 327
I am using Phonegap sample application to list all contacts with their names, but I am unable to do so.
Below is my onSuccess()
function. I am getting Found 7 contacts as a message in second line of this function. I am showing some of the results with my trials in getting the name.
function onSuccess(contacts) {
navigator.notification.alert('Found ' + contacts.length + ' contacts.');
var con = document.getElementById('con');
for (var i = 0; i < contacts.length; i++) {
navigator.notification.alert(contacts[i].name);
}
};
Trials :
<br/>
1). navigator.notification.alert(contacts[i])<br/>
Result ->
{"id":"0","displayName":"Andrew Hill","nickname":"Andrew Hill",
"phoneNumbers":["(206)5550001"],"emails":["Andy@fai=urthcoffee.com"],
"addresses":["Microsoft.Phone.UserData.ContactAddress"],
"urls":["www.fourthcoffee..com"]}
and like in the same foramt for other 6 contacts.
2). navigator.notification.alert(contacts[i].name)<br/>
Result ->
message
like same for all.
3). navigator.notification.alert(contacts[i].name[i].value)<br/>
Result ->
shows nothing.
How can I get the name field?
Upvotes: 2
Views: 5061
Reputation: 2615
Well, I have not tested for windows phone 7 but it is working fine with iOS/android, Tested on device and simulator both.
navigator.contacts.find(
['displayName', 'name','phoneNumbers'],
function(contacts){
var contact_name;
var contact_phone;
for( i = 0; i < contacts.length; i++) {
if(contacts[i].name.formatted != null && contacts[i].name.formatted != undefined ) {
contact_name = contacts[i].name.formatted;
contact_name = contact_name.replace(/'/g,"''");
if(contacts[i].phoneNumbers != null && contacts[i].phoneNumbers.length > 0 && contacts[i].phoneNumbers[0].value != null && contacts[i].phoneNumbers[0].value != undefined ) {
console.log( contacts[i].phoneNumbers[0].value );
contact_phone = contacts[i].phoneNumbers[0].value;
} else {
console.log( "--No Number-" );
contact_phone = "";
}
}
}
},function(error){
alert(error);
},{ filter:"", multiple:true }
);
Upvotes: 6