Obtain system contact information and display it on ListView

Source: Internet
Author: User

Requirement: display the following system contact information on a ListView. When you click the listView subitem, the contact name and number are returned to the previous activity and displayed.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1206164Q8-0.png "title =" con5.png "alt =" 121116696.png"/>


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131229/1206163156-1.png "title =" con1. png "alt =" 121839835.png"/>


Code Implementation


Required permissions: android. permission. READ_CONTACTS

Entity Contact:

package cn.contactinfo;public class Contact{    private String name;    private String phone;                                                                                                                                           public String getName()    {        return name;    }    public void setName(String name)    {        this.name = name;    }    public String getPhone()    {        return phone;    }    public void setPhone(String phone)    {        this.phone = phone;    }}

The main activity is the first interface.

Package cn. contactinfo; import android. app. activity; import android. content. intent; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. textView; public class ContactInfoActivity extends Activity {private Button btn_take; private TextView TV _show; public void onCreate (Bundle savedInstanceState) {super. onCreate (sa VedInstanceState); setContentView (R. layout. main); // find the corresponding control btn_take = (Button) findViewById (R. id. btn_take); TV _show = (TextView) findViewById (R. id. TV _show); // set the Click Event btn_take.setOnClickListener (new OnClickListener () {public void onClick (View v) {// for activity jump, and the returned result Intent intent = new Intent (ContactInfoActivity. this, SecondActivity. class); // set the result code to 0 startActivityForResult (intent, 0 );} });} // The onActivityResult () method protected void onActivityResult (int requestCode, int resultCode, Intent data) must be rewritten to return results. {// determine whether data is null if (data! = Null) {// determine whether the result code is 0 if (resultCode = 0) {// set the text TV _show.setText (data. getStringExtra ("content "));}}}}

ListView Interface

Package cn. contactinfo; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. contentResolver; import android. content. context; import android. content. intent; import android. database. cursor; import android.net. uri; import android. OS. bundle; import android. util. log; import android. view. view; import android. view. viewGroup; import android. widget. adapterView; import Droid. widget. adapterView. onItemClickListener; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView;/*** Contact List activity * @ author YSY */public class SecondActivity extends Activity {ListView lv_second; List <Contact> contacts; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. second); // obtain contact information c Ontacts = getContactInfo (); // find the corresponding control lv_second = (ListView) findViewById (R. id. lv_second); // set the adapter lv_second.setAdapter (new MyAdapter (this, contacts) for ListView; // set a click event for listView. When you click a subitem of ListView, you need to return the content of this subitem to the previous activity lv_second.setOnItemClickListener (new OnItemClickListener () {public void onItemClick (AdapterView <?> Arg0, View arg1, int position, long id) {// obtain the Contact object contact = (Contact) lv_second.getItemAtPosition (position) on the subitem ); // obtain the contact name and mobile phone number String content = "name:" + contact. getName () + "mobile phone number:" + contact. getPhone (); // creates an Intent object to pass data Intent intent = new Intent (); intent. putExtra ("content", content); // set the returned result setResult (0, intent); // close the current activity finish ();}});} /*** obtain the core content of the system Contact information) * @ return Contact Set */public List <Contact> getContactInfo () {// create a list set, all objects in the table are Contact List <Contact> contacts = new ArrayList <Contact> (); // obtain a ContentResolver instance. You can use the Activity member method to getContentResovler, for data query // ContentResolver Reference URL http://blog.sina.com.cn/s/blog_8ce4eca801010qk5.html ContentResolver resolver = getContentResolver (); // create two Uri object Uri uri = Uri. parse ("content: // com. android. contacts/raw_contacts "); Uri dataUri = Uri. parse ("content: // com. android. contacts/data "); // call the query method of the ContentResolver object to query data and return a Cursor object Cursor cursor = resolver. query (uri, null, null); // a two-layer loop is required here to find the contact name and number // (outer loop) loop cursor to retrieve the used field "_ id" while (cursor. moveToNext () {int id = c Ursor. getInt (cursor. getColumnIndex ("_ id"); Cursor dataCursor = resolver. query (dataUri, null, "raw_contact_id =? ", New String [] {String. valueOf (id)}, null); Contact contact = new Contact (); // memory loop, loop data1 and mimetype two fields content while (dataCursor. moveToNext () {String data = dataCursor. getString (dataCursor. getColumnIndex ("data1"); String mimetype = dataCursor. getString (dataCursor. getColumnIndex ("mimetype"); // if mimetype is a mobile phone number, put the data in contant if ("vnd. android. cursor. item/phone_v2 ". equals (mimetype) {contact. setPhone (data); // if mimetype is the name, put the data in contant} else if ("vnd. android. cursor. item/name ". equals (mimetype) {contact. setName (data) ;}// A contant contact is stored in the contacts set every cycle. add (contact); // close dataCursor. close ();} // close cursor. close (); return contacts;}/*** custom adapter * @ author YSY **/public class MyAdapter extends BaseAdapter {Context context; List <Contact> contacts; public MyAdapter (Context context, List <Contact> contacts) {this. contacts = contacts; this. context = context;} public int getCount () {return contacts. size ();} public Object getItem (int position) {return contacts. get (position);} public long getItemId (int position) {return position;} public View getView (int position, View convertView, ViewGroup parent) {Holder holder = null; if (convertView = null) {holder = new Holder (); convertView = View. inflate (SecondActivity. this, R. layout. item, null); holder. TV _name = (TextView) convertView. findViewById (R. id. TV _name); holder. TV _phone = (TextView) convertView. findViewById (R. id. TV _phone); convertView. setTag (holder);} else {holder = (Holder) convertView. getTag ();} holder. TV _name.setText (contacts. get (position ). getName (); holder. TV _phone.setText (contacts. get (position ). getPhone (); return convertView;} public class Holder {private TextView TV _name; private TextView TV _phone ;}}}

If you are not very familiar with ContentResolver, you can refer to the Web site http://blog.sina.com.cn/s/blog_8ce4eca801010qk5.html

I hope this will help you implement this function.



This article is from the "SEVEN" blog, please be sure to keep this source http://930307.blog.51cto.com/7950022/1338806

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.