1:Contact.java
package com.example.bean; public class Contact { private String name; private String phoneNumber; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } }
2:list_contact_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_margin="16dp"> <TextView android:id="@+id/tvContactName" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="18sp" android:singleLine="true" android:ellipsize="end"/> <TextView android:id="@+id/tvContactPhoneNumber" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:singleLine="true" android:ellipsize="end"/> </LinearLayout>
3:ContactAdapter.java
package com.example.adapter; import java.util.List; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; import com.example.bean.Contact; import com.example.contacttest.R; public class ContactAdapter extends ArrayAdapter<Contact>{ int resourceId; public ContactAdapter(Context context, int resourceId, List<Contact> list){ super(context,resourceId,list); this.resourceId = resourceId; } @Override public View getView(int position, View convertView, ViewGroup parent) { Contact contact = getItem(position); View view; ViewHolder holder; if(convertView==null){ view = LayoutInflater.from(getContext()).inflate(resourceId, null); holder = new ViewHolder(); holder.tvContactName = (TextView)view.findViewById(R.id.tvContactName); holder.tvContactPhoneNumber = (TextView)view.findViewById(R.id.tvContactPhoneNumber); holder.tvContactName.setText(contact.getName()); holder.tvContactPhoneNumber.setText(contact.getPhoneNumber()); view.setTag(holder); }else{ view = convertView; holder = (ViewHolder)view.getTag(); } return view; } class ViewHolder{ TextView tvContactName; TextView tvContactPhoneNumber; } }
4:MainActivity.java
package com.example.contacttest; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.database.Cursor; import android.os.Bundle; import android.provider.ContactsContract; import android.widget.ListView; import com.example.adapter.ContactAdapter; import com.example.bean.Contact; public class MainActivity extends Activity { List<Contact> list = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); list = new ArrayList<Contact>(); ListView lvContact = (ListView)findViewById(R.id.lvContact); ContactAdapter adapter = new ContactAdapter(this, R.layout.list_contact_item, list); lvContact.setAdapter(adapter); getContacts(); } void getContacts(){ Cursor cursor = null; try{ cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); if(cursor!=null){ Contact contact; while(cursor.moveToNext()){ contact = new Contact(); contact.setName(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME))); contact.setPhoneNumber(cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))); list.add(contact); } } }catch(Exception e){ e.printStackTrace(); }finally{ if(cursor!=null){ cursor.close(); } } } }
5:activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.contacttest.MainActivity$PlaceholderFragment" > <ListView android:id="@+id/lvContact" android:layout_width="match_parent" android:layout_height="match_parent"/> </RelativeLayout>
6:声明权限
<uses-permission android:name="android.permission.READ_CONTACTS"/>