How to implement contacts filter in android phonegap?

旧巷老猫 提交于 2019-12-11 12:34:40

问题


I have an option to send text message in my application. So there is a text box to enter a mobile number. I need to dynamically populate the filtered contacts if user entering the number. I just need similar to mobile phone contact search criteria. i.e, If user types 998 I need to display the list of filtered contacts based on given input 998.

To do this I have used navigator.contacts.find(), using this method I am able to get and find particular contacts. But not dynamic search criteria.

This is my text box,

 <input name="" id="numberTxt" placeholder="Enter A Mobile Number" value=""  type="tel" data-mini="true">

If user typing any number while typing I need to display filtered numbers. Is this possible? If yes, How can I do that? Any additional plugin are require? Any suggestions,

function onDeviceReady() {
    var options = new ContactFindOptions();
    options.filter = "";         
    options.multiple = true;     
    filter = ["displayName", "name"]; 
    navigator.contacts.find(filter, onSuccess, onError, options);
}

function onSuccess(contacts) {
       for (var i = 0; i < contacts.length; i++) {
            console.log("Display Name = " + contacts[i].displayName);
        }
}

function onError(contactError) {
     alert('onError!');
}

回答1:


I have installed the Contact Picker plugin from the following link,https://github.com/hazemhagrass/ContactPicker.git and modified the code code to little extent like,

window.plugins.ContactPicker.chooseContact(function(contactInfo) {
        var contactNumber = contactInfo.mobileNumber;
        document.getElementById("numberTxt").value= contactNumber ;
    });

I have added the mobileNumber parameter to contactInfo object so that I able to get any contact number from contact book and used in HTML.

ContactPicker.js modified code,

cordova.exec(function(contactInfo) {
        newContantInfo = {
            displayName: contactInfo.displayName,
            email: contactInfo.email,
            mobileNumber:contactInfo.mobileNumber, //included the mobile Number parameter
            phones: []
        };
        for (var i in contactInfo.phones) {
            if (contactInfo.phones[i].length)
            newContantInfo.phones = newContantInfo.phones.concat(contactInfo.phones[i]);
        };
        success(newContantInfo);
    }, failure, "ContactPicker", "chooseContact", []);

In ContactPickerPlugin.java,

 contact.put("email", email);
    contact.put("displayName", name);
    contact.put("mobileNumber", mobileNumber); // included the mobile Number in contact object
    contact.put("phones", phones);



回答2:


Indeed, you can filter by phone numbers. You need to be sure the "phoneNumbers" field name is listed in your array of fields to filter by. Below is an example I tested on Android to filter by phone number.

document.getElementById('searchContactsButton').onclick = function(e){
          var options = new ContactFindOptions();
          options.filter = document.getElementById('searchInput').value;
          options.multiple = true;
          var fieldsToFilter = ["phoneNumbers"];
          navigator.contacts.find(fieldsToFilter, 
            function(contacts) {
                alert('found ' + contacts.length + ' contacts');
          }, function(contactError) {
                alert('error: ' + contactError);
          }, options);
        }


来源:https://stackoverflow.com/questions/24892072/how-to-implement-contacts-filter-in-android-phonegap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!