Академический Документы
Профессиональный Документы
Культура Документы
Think Android
This post is for API levels 4 or lower and is a post to honor someones request
(https://thinkandroid.wordpress.com/requests/). The example will show you how to grab a
persons name, number, and prole picture, and set them in a view.
1 //QUERYFORTHEPEOPLEINYOURADDRESSBOOK
2 Cursorcursor=getContentResolver().query(People.CONTENT_URI,null,null
3 startManagingCursor(cursor);
4
5 //BINDTHENAMEANDTHENUMBERFIELDS
6 String[]columns=newString[]{People.NAME,People.NUMBER};
7 int[]to=newint[]{R.id.name_entry,R.id.number_entry};
8 SimpleContactAdaptermAdapter=newSimpleContactAdapter(this,R.layout.lis
9 this.setListAdapter(mAdapter);
1 <?xmlversion="1.0"encoding="utf8"?>
2 <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android
3 android:orientation="horizontal"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 android:id="@+id/list_entry_base">
7 <ImageView
8 android:id="@+id/profile_pic"
9 android:layout_width="wrap_content"
10 android:layout_height="wrap_content"/>
11 <LinearLayout
12 android:orientation="vertical"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 android:paddingLeft="12dip">
16 <TextView
https://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-prole-picture/ 1/5
4/7/2016 Retrieving Contact Information (Name, Number, and Prole Picture) | Think Android
17 android:id="@+id/name_entry"
18 android:layout_width="wrap_content"
19 android:layout_height="wrap_content"/>
20 <TextView
21 android:id="@+id/number_entry"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"/>
24 </LinearLayout>
25 </LinearLayout>
And nally the code for the SimpleContactAdapter (which is our custom cursor adapter for
more info see https://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/
(https://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/)) should look like:
1 @Override
2 publicViewnewView(Contextcontext,Cursorcursor,ViewGroupparent)
3
4 Cursorc=getCursor();
5
6 finalLayoutInflaterinflater=LayoutInflater.from(context);
7 Viewv=inflater.inflate(layout,parent,false);
8
9 intidCol=c.getColumnIndex(People._ID);
10 intnameCol=c.getColumnIndex(People.NAME);
11 intnumCol=c.getColumnIndex(People.NUMBER);
12
13 Stringname=c.getString(nameCol);
14 Stringnumber=c.getString(numCol);
15 longid=c.getLong(idCol);
16
17 //SETTHENAMEHERE
18 TextViewname_text=(TextView)v.findViewById(R.id.name_entry);
19 if(name_text!=null){
20 name_text.setText(name);
21 }
22
23 //SETTHEPROFILEPICTURE
24 ImageViewprofile=(ImageView)v.findViewById(R.id.profile_pic);
25 if(profile!=null){
26 //RETRIEVETHECONTACTPHOTOASABITMAP
27 Uriuri=ContentUris.withAppendedId(People.CONTENT_URI,id);
28 Bitmapbitmap=People.loadContactPhoto(context,uri,R.drawab
29
30 //SETITHEREINTHEIMAGEVIEW
31 profile.setImageBitmap(bitmap);
32 }
33
34 //SETTHECONTACTPHONENUMBER
35 TextViewnumber_text=(TextView)v.findViewById(R.id.number_entry
36 if(number_text!=null){
37 number_text.setText(number);
38 }
39
40 returnv;
41 }
https://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-prole-picture/ 2/5
4/7/2016 Retrieving Contact Information (Name, Number, and Prole Picture) | Think Android
And putting all those together should do it! Now you should be able to recreate your phones
address book and put the name, number, and also the contacts local photo.
jwei
Abouttheseads(https://wordpress.com/abouttheseads/)
REPLY
2. Android Phones PERMALINK
January 24, 2010 10:18 pm
Nice inspiring blog, dude. I enjoy your posting. I am about to create blog about mobile
phones, too.
Thanks!
REPLY
3. ezyclie PERMALINK
January 31, 2010 7:06 pm
Hi,
Can i get the fully source code for this example ? if yes, please send it to
e_zy_clie@yahoo.com
Thanks
REPLY
4. Pendroid PERMALINK
February 14, 2010 10:09 pm
https://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-prole-picture/ 3/5
4/7/2016 Retrieving Contact Information (Name, Number, and Prole Picture) | Think Android
REPLY
jwei512 PERMALINK*
February 14, 2010 10:12 pm
hey pendroid
do u mean to put up snap shots of the emulator? and do u mean for this particular post,
or in general?
jwei
REPLY
5. Mike PERMALINK
April 21, 2010 7:31 pm
hi jwei512,
this is cool. this helps me a lot. but have a small issue, if user has a mobile number as well
as a HOME or a WORK number too how to retrieve them and how to display them??
regards,
mike
REPLY
6. Sulabh PERMALINK
October 7, 2010 12:01 am
Hey,
nice example. this is working ne but fetching contact image only for rst contact.
REPLY
7. Beachsnow PERMALINK
November 9, 2010 12:15 pm
Hi,
I have the same problem as Sulabh.
There must be a bug in the code (which I have not jet found) because I the image is only
correct for the rst two contacts and just repates this picture for the next contacts that have
a image. Please help to x it!
Regards Stefan
REPLY
Beachsnow PERMALINK
November 9, 2010 12:39 pm
If I turn the device, which causes a onCreate(), the correct images are shown for the
current view.
https://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-prole-picture/ 4/5
4/7/2016 Retrieving Contact Information (Name, Number, and Prole Picture) | Think Android
REPLY
8. web design tipperary, ireland PERMALINK
March 21, 2011 6:09 am
cheers man, just what i was looking for, most info is for later API versions
REPLY
9. satya PERMALINK
March 22, 2011 7:08 am
Your example works very ne in emulator but it is displaying empty screen in real device
REPLY
10. Baradwaj PERMALINK
December 20, 2012 10:31 am
Only images of the rst two contacts were displayed what to do
REPLY
Trackbacks
Follow
https://thinkandroid.wordpress.com/2010/01/19/retrieving-contact-information-name-number-and-prole-picture/ 5/5