Am developing a android application using phonegap(Cordova 2.0.0). I need the retrieve the device contacts. I have tried with code given this doc
http://docs.phonegap.com/en/2.0.0/cordova_contacts_contacts.md.html#Contacts
my code as follows
$("#shareoptions3").live('click',function(){
var options = new ContactFindOptions();
options.multiple = true;
var fields = ["displayName","phoneNumbers"];//["displayName", "name","phoneNumbers"];
navigator.contacts.find(fields, onContactSuccess, onContactError, options);
});
function onContactSuccess(contacts) {//alert(contacts.length);
for (var i=0; i<contacts.length; i++) {
// display phone numbers
for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
alert("Type: " + contacts[i].phoneNumbers[j].type + "\n" +
"Value: " + contacts[i].phoneNumbers[j].value + "\n" +
"Preferred: " + contacts[i].phoneNumbers[j].pref);
}
}
};
// onError: Failed to get the contacts
//
function onContactError(contactError) {
console.log('Error in getting contacts!');
}
I am getting message in Logcat like this :
Error in success callback: Contacts3 = TypeError: Cannot read property 'length' of null at file:///android_asset/www/JS/cordova-2.0.0.js:258
Please help me to sort out this problem.
One of your contact may not have a telephone number, and that is why you'll get a null value instead of a phone number.
So, in your for loop, one of the contacts[i].phoneNumbers.length wil generate an error.
I suggest you to first check if the phoneNumbers is null or not, before displaying / alerting it, by using:
if(contacts[i].phoneNumbers != null)
In the end, you may try something like this:
$("#shareoptions3").live('click',function(){
var options = new ContactFindOptions();
options.multiple = true;
var fields = ["displayName","phoneNumbers"];//["displayName", "name","phoneNumbers"];
navigator.contacts.find(fields, onContactSuccess, onContactError, options);
});
function onContactSuccess(contacts) {//alert(contacts.length);
for (var i=0; i<contacts.length; i++) {
// display phone numbers
if(contacts[i].phoneNumbers != null) // Checking if not null
for (var j=0; j<contacts[i].phoneNumbers.length; j++) {
alert("Type: " + contacts[i].phoneNumbers[j].type + "\n" +
"Value: " + contacts[i].phoneNumbers[j].value + "\n" +
"Preferred: " + contacts[i].phoneNumbers[j].pref);
}
}
};
// onError: Failed to get the contacts
//
function onContactError(contactError) {
console.log('Error in getting contacts!');
}
Hope this helps, let me know if this works for you.
Related
Recently i was creating my own live-template, i was customizing for loop, here is how default for loop is given in live-template.
for(int $INDEX$ = 0; $INDEX$ < $LIMIT$; $INDEX$++) {
$END$
}
but i want my method's first argument inplace of $LIMIT$, how can i do that?
public void getList(ArrayList<String> list)
{
}
then my for loop shoulde be
for(int i = 0; i < list.size; i++) {
...
}
I have seen template of logm, but it is printing all arguments of method
groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
You can add the following live template:
for(int $INDEX$ = 0; $INDEX$ < $VAR$.size(); $INDEX$++){
$END$
}
Then go to right button "Edit variables" and put the following predefined methods in the expressions fields:
You can find all predefined methods in Jetbrains documentation
I am working on android application in phonegap-3.1.0
I want to use phone contacts in my application, So I have refer this Document.
Successfully installed the plugins for contacts
When I remove saved contact(save from javascript code), It alerts Removal Success
But when I go into the contacts, it is still not removed from here,
Every time I try, it saves the contact but not not removed since alerts like Removal Success,
What should I Do...
SO I need help on it, Why the contact can't be remove
I have created an app for contacts insert and delete
You can fork on github-> xxbinxx/phoneGap-ContactsApp-Android. you can definitely solve your issue after it.
I have used contact ID's for the deletion purpose.
Here's the short code...
var app ={
/********************SOME OTHER CODE*************************/
openContacts: function() {
app.initialize();
var options = new ContactFindOptions();
options.filter = "";
options.multiple = true;
var fields = ["*"]; //"*" will return all contact fields
navigator.contacts.find(fields, app.onSuccess, app.onError, options);
},
// Write contacts in DOM
onSuccess: function(contacts) {
var li = '';
$.each(contacts, function(key, value) {
if (value.name) {
$.each(value.name, function(key, value) {
if (key === 'formatted') {
name = value;
}
});
}
if (value.note) {
note = value.note;
}
if (value.id) {
id = value.id;
}
console.log("id : " + id + "-> name : " + name + " -> note : " + note);
li += '<li style="text-decoration:none;"><b>Name</b>: ' + name + '<div class="removeIcon pullRight" onclick="app.removeThisContact(\'' + id + '\',\'' + name + '\')"> </div><br><b> Note:</b> ' + note + '</li>';
}); // NOTICE the ID is passed as PARAMETER to remove specific contact.
$("#contact").html(li);
},
onError: function(contactError) {
alert('onError!' + contactError.code);
},
removeThisContact: function(id, name) {
console.log("removing contact : " + name);
options = new ContactFindOptions(); // find the contact to delete
options.filter.id = id;
options.multiple = "true";
var fields = ["displayName", "name"]; // you can take any..
navigator.contacts.find(fields, deleteThis, app.onError, options);
function deleteThis(contacts) {
var contact = contacts.pop();
// logging things to troubleshoot.
console.log('inside deleteThisContact: parameter passed: '+ contacts);
console.log("popped out:" +contact);
contact.remove(function() {
$('#status-area')
.flash_message({
text: 'Contact Removed!',
how: 'append'
});
app.openContacts();
}, null);
},
deleteAllTheContacts: function() {
var deleteContact = function(contacts) {
console.log("length = " + contacts.length);
// No contacts left, stop saving
if (contacts.length == 0) {
console.log("All contacts removed");
return;
}
var contact = contacts.pop();
contact.remove(function() {
deleteContact(contacts);
}, null);
};
navigator.contacts.find(["*"], deleteContact, app.onError, {
"multiple": true
});
},
/********************SOME OTHER CODE*************************/
}
$.fn.flash_message = function(options) {
//flash your message
}
Hope this will help you. :)
Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());
Is there any way to access automatically any Log in Logcat by a double click ?
Actually, when there is an error crashing my Android Application, I can double click on the line saying for instance
at com.myapp.mypackage$Class.function(File.java:117)
And by Double-clicking on this line, I am automatically redirected to the related line of my code.
But, when I try to generate the same line in another Log, example :
Log.e("TAG", "at com.myapp.mypackage$Class.function(File.java:117)");
The Double-Click doesn't work anymore ...
Any ideas ?
If you want to create a log in logcat that can be clicked and go to your line use the following method to create it:
Enjoy!
public static void showLogCat(String tag, String msg) {
StackTraceElement[] stackTraceElement = Thread.currentThread()
.getStackTrace();
int currentIndex = -1;
for (int i = 0; i < stackTraceElement.length; i++) {
if (stackTraceElement[i].getMethodName().compareTo("showLogCat") == 0)
{
currentIndex = i + 1;
break;
}
}
String fullClassName = stackTraceElement[currentIndex].getClassName();
String className = fullClassName.substring(fullClassName
.lastIndexOf(".") + 1);
String methodName = stackTraceElement[currentIndex].getMethodName();
String lineNumber = String
.valueOf(stackTraceElement[currentIndex].getLineNumber());
Log.i(tag, msg);
Log.i(tag + " position", "at " + fullClassName + "." + methodName + "("
+ className + ".java:" + lineNumber + ")");
}
If you don't mind the clutter in your log, you can easily just add a new Exception() to the log message
Log.e("TAG", "Looky here see", new Exception());
I am having a problem when I create an array of strings, this only happens in 2.1 android api level 7 or lower and i need to install the application on a device with exactly this configuration, any idea how to solve the problem?
Below the source code, the message that pops up on screen and also logcat's message.
CODE:
private String[] fillPedidosName() {
TipoPedidoDAO tipoDAO = null;
try {
tipoDAO = new TipoPedidoDAO();
pedidosList = tipoDAO.selectAll();
String[] temp = new String[pedidosList.size()];
for (int i = 0; i < pedidosList.size(); i++) {
if (pedidosList.get(i) != null) {
temp[i] = pedidosList.get(i).getDescricao().toString();
}
}
return temp;
} catch (Exception ex) {
MyLoger.logar(ex);
return null;
} finally {
if (tipoDAO.getDB().isOpen()) {
tipoDAO.closeConnection();
}
}
}
THE MESSAGE THAT POPS UP DEBUGING:
Exception processing async thread queue
Exception processing async thread queue
java.lang.UnsupportedOperationException
lOGCAT'S MESSAGE:
03-03 17:57:57.124: ERROR/jdwp(1267): REQ: UNSUPPORTED (cmd=2/11 dataLen=8 id=0x0012ba)
You are probably not using a List that supports get(int).
Try changing your List implementation to ArrayList. When you create your list:
List myList = new ArrayList()
This is probably happening inside tipDAO.selectAll().
I had this problem. I got it fixed.
When working with Arrays of your Objects, make sure you have defined a constructor in the object file.
This piece of code was creating the error
List<Prediction> predictions = new ArrayList<Prediction>();
The fix.
Prediction class file was missing a constructor. After putting in a default constructor, the error is gone for me.
package com.thuptencho.torontotransitbus.models;
public class Prediction {
public String epochTime = "", seconds = "", minutes = "", isDeparture = "", affectedByLayover = "", branch = "",
dirTag = "", vehicle = "", block = "", tripTag = "";
//this constructor was missing..after coding this constructor. the error was gone.
public Prediction(){
super();
}
#Override
public String toString() {
return "epochTime:" + this.epochTime + " seconds:" + this.seconds + " minutes:" + this.minutes
+ " isDeparture:" + this.isDeparture + " affectedByLayover:" + this.affectedByLayover + " branch:"
+ this.branch + " dirTag:" + this.dirTag + " vehicle:" + this.vehicle + " block:" + this.block
+ " triptag:" + this.tripTag;
}
}