[Reprinted] Android 2.3.3 API reads the phone number instance in the address book

Source: Internet
Author: User

I recently started to learn about Android, mainly focusing on the secrets of Android Application Development. In chapter 3rd, example_03_02 is an example of reading the name and phone number of contacts in the address book. However, due to API 2.0, each contact can have multiple phone numbers (such as mobile phones, residential buildings, companies, and faxes). The original Instance of the book reports an error in API 2.0.

Example_03_02 in the bookCode:

View code

 Package  Com. Yarin. Android. examples_03_02;  Import  Android. App. activity;  Import  Android. content. contentresolver;  Import  Android. database. cursor;  Import  Android. OS. Bundle; Import  Android. provider. contactscontract;  Import  Android. provider. contactscontract. phonelookup;  Import  Android. util. log;  Import  Android. widget. textview;  Import  Org. Apache. commons. Lang. stringutils;  Public   Class Activity01 Extends  Activity { Public   Void  Oncreate (bundle savedinstancestate) {textview TV = New Textview ( This  ); String = "" ;  Super  . Oncreate (savedinstancestate );  //  Obtain the contentresolver object. Contentresolver Cr = Getcontentresolver ();  // Get the cursor of the first item in the phone book Cursor cursor = Cr. Query (contactscontract. Contacts. content_uri, Null , Null , Null , Null  ); Log. I ( "Tag" , Integer. tostring (cursor. getcolumncount (); log. I ( "Tag", stringutils. Join (cursor. getcolumnnames (),"," ));  //  Move the cursor down          While (Cursor. movetonext ()){  //  Get contact name              Int Namefieldcolumnindex = Cursor. getcolumnindex (phonelookup. display_name); string contact = Cursor. getstring (namefieldcolumnindex );  //  Get the phone number (this method reports an error because one contact may have multiple phone numbers)  //  Int numberfieldcolumnindex = cursor. getcolumnindex (phonelookup. _ id );  // String number = cursor. getstring (numberfieldcolumnindex );  //  String + = (contact + ":" + number + "\ n ");  //  New method for obtaining phone numbers String contactid = Cursor. getstring (cursor. getcolumnindex (contactscontract. Contacts. _ id); cursor phone = Cr. Query (contactscontract. commondatakinds. Phone. content_uri, Null , Contactscontract. commondatakinds. Phone. contact_id + "=" + contactid, Null , Null  ); While  (Phone. movetonext () {string phonenumber = Phone. getstring (phone. getcolumnindex (contactscontract. commondatakinds. Phone. Number); string + = (Contact + ":" + phonenumber + "\ n" ) ;}} Cursor. Close ();  //  Set the content displayed in textview  TV. settext (string );  //  Display to screen  Setcontentview (TV );}} 

The original code has been annotated by me and the correct code has been modified.

What if we only want to get the mobile phone number?

 
Cursor phone = Cr. Query (contactscontract. commondatakinds. Phone. content_uri,Null, Contactscontract. commondatakinds. Phone. contact_id+ "=" + Contactid,Null,Null);

Change the preceding statement:

 
Cursor phone = Cr. Query (contactscontract. commondatakinds. Phone. content_uri,Null, Contactscontract. commondatakinds. Phone. contact_id+ "=" + Contactid + "and" +Contactscontract. commondatakinds. Phone. Type+ "=" + Contactscontract. commondatakinds. Phone. type_mobile,Null,Null);

Contactscontract. commondatakinds. Phone. type indicates the contact phone type, which corresponds to the following:
Type_mobile: mobile phone number
Type_home: residential phone number
Type_work: company phone number

 

Add the main. XML code.

 

 <?  XML version = "1.0" encoding = "UTF-8"  ?>  <  Linearlayout  Xmlns: Android  = "Http://schemas.android.com/apk/res/android"  Android: Orientation  = "Vertical" Android: layout_width  = "Fill_parent"  Android: layout_height  = "Fill_parent"      >  <  Textview  Android: layout_width  = "Fill_parent"  Android: layout_height  = "Wrap_content"  Android: Text  = "@ String/hello"      />  </ Linearlayout  > 

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.