I would like to fetch contacts and populate them in a listview, I read the flutter documentation:
// Get all contacts on device
Iterable<Contact> contacts = await ContactsService.getContacts();
How can I access the contact name?
Contact is a class, and its fields are the following :
String displayName, givenName, middleName, prefix, suffix, familyName;
// Company
String company, jobTitle;
// Email addresses
Iterable<Item> emails = [];
// Phone numbers
Iterable<Item> phones = [];
// Post addresses
Iterable<PostalAddress> postalAddresses = [];
// Contact avatar/thumbnail
Uint8List avatar;
you can access the contact name by iterating throw the array contacts , and access each fields , as the following :
contacts.forEach((contact){
print(contact.displayName);
});
To get phone number ...this is my code:
import 'package:contacts_service/contacts_service.dart';
import 'package:permission_handler/permission_handler.dart';
void printContactsNumber() {
Iterable<Contact> contacts = await ContactsService.getContacts(withThumbnails: false);
List<Contact> contactsList = contacts.toList();
// now print first 50 number's contancts
for (int i = 0; i < 50; i++) {
contactsList[0].phones.first.value.toString()
}
}
Related
i am creating app in which i have to compare my device contact to server data contact.i am doing this but only last data is coming,but i want to make comparison with every device contact to server contact.I am able to fetch data from device and from server but i don't know how to compare.
Here phonenumber is no coming from phonecursor and second is server data.
Objects.equals(phoneNumber, jsonObject1.getString("mobile"))
after the comparison i want to select any five contact from list and send it to another fragment.i don't know how to select item from recyclerview list.
first you have to store your all contact into DB.
then,
ArrayList<String> foundNumb = new ArrayList();
for (int A = 0; A < DB_Contact_List.size(); A++) {
// get contact from DB list one by one
DBContactBean dbDataBean = DB_Contact_List.get(A);
String DbNumb = dbDataBean.getmNumber().replaceAll("\\s+", "");
String ServrNumb = "";
ServerContactBean serverDataBeanM = null;
boolean found = false;
int BBB = 0;
if (!DbNumb.equals("")) {
innerLoop:
for (int B = 0; B < Server_Contact_List.size(); B++) {
// get contatct from Server list one by one
ServerContactBean serverDataBean = Server_Contact_List.get(B);
BBB = B;
serverDataBeanM = serverDataBean;
ServrNumb = serverDataBean.getPh_phone().replaceAll("\\s+", "");
// compare it
if (DbNumb.equalsIgnoreCase(ServrNumb)) {
found = true;
break innerLoop;
}
}
if (found) {
// if found do your task
foundNumb.add(DbNumb);
} else {
// not found
}
}
}
Check out following classes:
class Person{
int id;
String name;
RealmList<Mail> mails;
...
}
class Mail{
int id;
String content;
...
}
I have a Person object (ie: mPerson) and I am accessing all Mails of the Person object by mPerson.getMails(). Till here everything is cool.
Here is the question: Is there way to query over the returned list such as findAllSortedAsync()?
Just use RealmList.where() to create a query. You can find document here
For example:
RealmList<Mail> mails = person.getMails();
RealmResults<Mail> results = mails.where().equalTo("id", 1).findAllSortedAsync();
RealmResults<Contact> contacts = mRealm.where(Contact.class).findAll();
int size = contacts.size();
for (int i = 0;i<size;i++){
Contact contact = contacts.get(i);
RealmList<EMail> eMails = contact.emails;
}
So I am working on a project where I need to get the user's contact list (Specifically the Name, email address, and location of the contact details), put that into a list, and then use that list in an autocomplete view so they can start typing a name and it will filter it out.
My code works just fine, it all compiles and all runs without error. The problem is that it is VERY slow. For someone who has 10 contacts, this will likely not take long, but my phone holds 1700 contacts in it so this entire process takes upwards of 2 minutes to complete... which is horrendous!
Here is the current working code below (I cut out the code for the location adding as it was wordy):
public static List<MyObject> getContactList(){
List<MyObject> contactList = new ArrayList<>();
Cursor people = MyApplication.getAppContext().getContentResolver().
query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (people.moveToNext()) {
String email = "";
String location = "";
String phone = "";
String contactName = people.getString(people.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
String contactId = people.getString(people.getColumnIndex(
ContactsContract.Contacts._ID));
if(contactId != null){
Cursor contactsEmails = getSpecificEmailsCursor(contactId);
while (contactsEmails.moveToNext()){
//For now, just setting it to the last email
email = contactsEmails.getString(contactsEmails.getColumnIndex(
ContactsContract.CommonDataKinds.Email.DATA));
phone = contactsEmails.getString(contactsEmails.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
}
contactsEmails.close();
MyObject person = new MyObject();
try{
person.setEmail(email);
person.setName(contactName);
person.setPhone(phone);
} catch (NullPointerException e){
L.m("Null pointer hit on person");
}
if(contactName != null && email != null){
contactList.add(person);
}
}
}
people.close();
return contactList;
}
Does anyone have any recommendations on how to either speed this process up, or, a better way to go about what I am trying to accomplish? Thanks!
-Sil
Using phonegap, I can get/filter a single contact from contact list. But how to update (add/remove) phone number field. Please help. Thanks alot.
Lets say 1 got a contact name John Smith with 2 phone number [('Home', '1111'), ('Work', '2222')].
When I try to remove the 'Work' number, just keep the 'Home' one. First get the contact, try to remove all number, then add the 'Home' number but I always get both 3 numbers [('Home', '1111'), ('Work', '2222'), ('Home', '1111')]
Weir that if I try to remove all number, then add nothing, it really remove all the number from contact ?
Here is my code
var phoneNumbers = [];
for (...){
phoneNum = {
type: ...,
value: ...,
pref: false
};
phoneNumbers.push(phoneNum);
}
contact = contacts_list[index]; //get the contact need to edit
//try to remove all current phone number
if (contact.phoneNumbers){
for (var i = 0; i < contact.phoneNumbers.length; i++){
delete contact.phoneNumbers[i];
//contact.phoneNumbers[i] = null; //i try this too
//contact.phoneNumbers[i] = []; //i try this too
}
}
//set new phone number
contact.phoneNumbers = phoneNumbers;
contact.save(...)
I also try create a new contact with only 1 number [('Home', '1111')], set id and rawId as same as i contact object I need to update, then save(). But i still get the same result [('Home', '1111'), ('Work', '2222'), ('Home', '1111')]
var contact = navigator.contacts.create();
var phoneNumbers = [];
phoneNumbers[0] = new ContactField('Home', '1111', false);
contact.phoneNumbers = phoneNumbers;
contact.id = ...
contact.rawId = ...
contact.save(...);
this also
contact = contacts_list[index]; //get the contact need to edit
//try to remove all current phone number
if (contact.phoneNumbers){
for (var i = 0; i < contact.phoneNumbers.length; i++){
delete contact.phoneNumbers[i];
//contact.phoneNumbers[i] = null; //i try this too
//contact.phoneNumbers[i] = []; //i try this too
}
}
var phoneNumbers = [];
phoneNumbers[0] = new ContactField('Home', '1111', false);
contact.phoneNumbers = phoneNumbers;
contact.save(...)
In the contact plugin of cordova, you can save the contact passing the original contact id, it will update the contact details in the database.
Here is an example:
//Set the options for finding conact
var options = new ContactFindOptions();
options.filter = 'Bob'; //name that you want to search
options.multiple = false;
var fields = ["id","displayName", "phoneNumbers"];
navigator.contacts.find(fields, sucessUpdate, onError, options);
function sucessUpdate(contacts) {
var contact = contacts[0]; //found contact array must be one as we disabled multiple false
// Change the contact details
contact.phoneNumbers[0].value = "999999999";
contact.name = 'Bob';
contact.displayName = 'Mr. Bob';
contact.nickname = 'Boby'; // specify both to support all devices
// Call the "save" function on the object
contact.save(function(saveSuccess) {
alert("Contact successful update");
}, function(saveError){
alert("Error when updating");
});
}
function onError(contactError)
{
alert("Error = " + contactError.code);
}
This is my code for getting the contact names from my device. The problem I'm having is that in my listview, "elements" will display every name in contact list. Any ideas of how I can remove the names that have no SMS?
// converts contacts from cursor to arraylist
nameList = new ArrayList<String>();
cursor = getContacts();
while(cursor.moveToNext()){
nameList.add(cursor.getString(cursor.getColumnIndex(
ContactsContract.Data.DISPLAY_NAME )));
}
// convert arraylist to string array
name = new String[nameList.size()];
name = nameList.toArray(name);
// new arraylist for after contacts with no messages are removed
elements = new ArrayList<String>();
//convert back to an arraylist
for(int i = 0; i < name.length; i++){
elements.add(name[i]);
}
I would imagine that you need to retrieve the sms list and then do comparison to each contact number to find the actual number of SMS messages per contact. Once you have that you can remove the contacts from the list who have 0 messages.
This is a good link regarding working with SMS