获取联系人displayName和phoneNumbers

时间:2017-02-09 05:38:23

标签: angular ionic2

我正在使用本地离子联系人来获取电话中的联系人列表

.TS:

 import { Contacts } from 'ionic-native';
   /////////
 export class ContactPage {
   contactsfound = []

 constructor(public navCtrl: NavController) {
    Contacts.find(["displayName", "phoneNumbers"], {multiple: true}).then((contacts) => {
     this.contactsfound = contacts;
    })
   }
 }

html的:

 <ion-content>
  <ion-list>
   <ion-list-header>Follow us on Twitter</ion-list-header>
     <ion-item *ngFor="let item of contactsfound">
       {{ item.displayName }}
       <p>{{ item.phoneNumbers }}</p> 
     </ion-item>
  </ion-list>
 </ion-content>

我正确地获取了displayName的列表但phoneNumbers我获得了[object Object] int range = (maximum - minimum) + 1; int out = (int) (Math.random() * range) + minimum; System.out.println("random " + out); 这该怎么做?? 此外,我希望在列表的左侧有一个字母,其中表示以该字母开头的一组联系人的第一个字母(例如,列表组然后滚动B列表组)....这将是伟大的如果有人提供有关此的见解。感谢名单

2 个答案:

答案 0 :(得分:0)

我已经解决了以下问题:

.TS:

import { Contacts } from 'ionic-native';
 /////////
export class ContactPage {
 contacts = []
 groupedContacts = []

constructor(public navCtrl: NavController) {
   Contacts.find(["displayName", "phoneNumbers"], {multiple: true, hasPhoneNumber: true}).then((contacts) => {

    for (var i=0 ; i < contacts.length; i++){
     if(contacts[i].displayName !== null){
       var obj = {};
       obj["name"] = contacts[i].displayName;
       obj["number"] = contacts[i].phoneNumbers[0].value;
       this.contacts.push(obj)    // adding in separate array with keys: name, number
     }      
    }

    this.groupContacts(this.contacts);
  })
}

groupContacts(contacts){

  let sortedContacts = contacts.sort(function(a, b){
    if(a.name < b.name) return -1;
    if(a.name > b.name) return 1;
    return 0;
   });

  let currentLetter = false;
  let currentContacts = [];

  sortedContacts.forEach((value, index) => {
    if(value.name.charAt(0) != currentLetter){
      currentLetter = value.name.charAt(0);

      let newGroup ={
        letter: currentLetter,
        contacts:[]
      };

      currentContacts = newGroup.contacts;
      this.groupedContacts.push(newGroup);
    }
    currentContacts.push(value);
  });
}

.html:

 <ion-content>
  <ion-item-group *ngFor="let group of groupedContacts">
    <ion-item-divider color="light">{{group.letter}}</ion-item-divider>
     <ion-item *ngFor="let contact of group.contacts" class="contactlist">
       {{contact.name}}
       <p>{{contact.number}}</p>
     </ion-item>
  </ion-item-group>

答案 1 :(得分:0)

我在列表中显示带有数字的搜索联系人时遇到了同样的问题。尝试使用此编辑您的第一篇文章。

html的

 <ion-item *ngFor="let item of contactsfound">
   {{ item.displayName }}
   <p>{{ item.phoneNumber[0].value }}</p> 
 </ion-item>

显然,此方法仅显示每个联系人的一个号码。对于多个数字,只需添加另一个item.phoneNumber [1] .value等等。