How can I fetch all contacts on an Android phone, in an Activity? -


how can fetch contacts on android phone, in activity?

  1. make contact class
  2. get contacts list contentresolver()
  3. parse contacts list , fill arraylist<contact>
  4. make contactarrayadapter

contactarrayadapter sample

//////////////////////////////////////////////////////////////////////////// private class contactsadapter extends arrayadapter<contact> {      private final int _resourceid;     private final layoutinflater _inflater;     private final context _context;      public contactsadapter(context context, int textviewresourceid, list<contact> contacts) {         super(context, textviewresourceid, contacts);         _context = context;         _resourceid = textviewresourceid;         _inflater = layoutinflater.from(context);     }      @override     public view getview(int position, view v, viewgroup parent) {         viewholder holder;         if (v == null) {             v = _inflater.inflate(_resourceid, null);             holder = new viewholder();             holder.tv_name = (textview) v.findviewbyid(r.id.tv_name);             holder.tv_phonenumber = (textview) v.findviewbyid(r.id.tv_phonenumber);             holder.iv_photoid = (imageview) v.findviewbyid(r.id.iv_photo);             v.settag(holder);         }         else {             holder = (viewholder) v.gettag();         }          contact contact = getitem(position);         holder.tv_name.settext(contact.getname());         holder.tv_phonenumber.settext(contact.getphonenumber());          bitmap bm = openphoto(contact.getphotoid());         if (bm != null) {             holder.iv_photoid.setimagebitmap(bm);         }         else {             holder.iv_photoid.setimagedrawable(getresources()                     .getdrawable(r.drawable.ic_item_profile_medium));         }          return v;     }      private bitmap openphoto(long contactid) {         uri contacturi = contenturis.withappendedid(contacts.content_uri, contactid);         inputstream input = contactscontract.contacts                 .opencontactphotoinputstream(_context.getcontentresolver(),                         contacturi);          if (input != null) {             return bitmapfactory.decodestream(input);         }          return null;     }      private class viewholder {         imageview iv_photoid;         textview tv_name;         textview tv_phonenumber;     }  } 

assign adapter listview in oncreate() or onresume() of activity.

listview contactlist = (listview) findviewbyid(r.id.list_contact); contactsadapter adapter = new contactsadapter(youractivity.this,             r.layout.list_contact, getcontactlist()); contactlist.setadapter(adapter); 

i found above source googling can't remember reference site. sorry that.


Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -