How can I fetch all contacts on an Android phone, in an Activity? -
how can fetch contacts on android phone, in activity?
- make contact class
- get contacts list
contentresolver()
- parse contacts list , fill
arraylist<contact>
- 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
Post a Comment