<?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TableRow> <TextView android:id="@+id/_id" android:layout_height="wrap_content" android:layout_width="30px" /> <TextView android:id="@+id/name" android:layout_height="wrap_content" android:layout_width="300px" /> </TableRow> </TableLayout>
布局文件主页显示 ----- main.xml
<?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:id="@+id/mainInfo" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机联系人列表"/> <ListView android:id="@+id/contactsList" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </LinearLayout>
package org.lxh.demo; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.ContactsContract; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class ContactsDemo extends Activity { // 既然要查询,查询返回的就是结果 private Cursor result = null; // 定义ListView组件 private ListView contactsList = null; private List<Map<String, Object>> allContacts = null; private SimpleAdapter simple = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.main); this.contactsList = (ListView) super.findViewById(R.id.contactsList); this.result = super.getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null, null, null, null); // 将结果集交给容器管理 super.startManagingCursor(this.result); // 实例化List集合 this.allContacts = new ArrayList<Map<String, Object>>(); // 取出结果集中的每一个内容 for (this.result.moveToFirst(); !this.result.isAfterLast(); this.result.moveToNext()) { Map<String, Object> contact = new HashMap<String, Object>(); contact.put("_id", this.result.getInt(this.result .getColumnIndex(ContactsContract.Contacts._ID))); contact.put("name", this.result.getString(this.result .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))); this.allContacts.add(contact); } this.simple = new SimpleAdapter(this, this.allContacts, R.layout.contacts, new String[] { "_id", "name" }, new int[] {R.id._id, R.id.name }); this.contactsList.setAdapter(this.simple); // 注册菜单 super.registerForContextMenu(this.contactsList); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item .getMenuInfo(); // 取得操作位置 int position = info.position; String contactsId = this.allContacts.get(position).get("_id").toString() ; // 进行菜单的操作 switch(item.getItemId()){ case Menu.FIRST + 1: // 查看 String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?"; String[] phoneSelectionArgs = new String[] { contactsId}; Cursor c = super.getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, phoneSelection, phoneSelectionArgs, null); StringBuffer buf = new StringBuffer() ; buf.append("电话号码是:") ; for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) { buf.append(c.getString(c .getColumnIndex(ContactsContract. CommonDataKinds.Phone.NUMBER))).append("、"); } Toast.makeText(this, buf, Toast.LENGTH_SHORT).show(); break ; case Menu.FIRST + 2: // 删除 super.getContentResolver().delete( Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, contactsId), null, null); // 删除集合数据项 this.allContacts.remove(position) ; // 通知改变 this.simple.notifyDataSetChanged() ; Toast.makeText(this, "数据已删除!", Toast.LENGTH_SHORT).show(); break ; } return super.onContextItemSelected(item); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // 创建菜单 super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("联系人操作"); menu.add(Menu.NONE, Menu.FIRST + 1, 1, "查看详情"); menu.add(Menu.NONE, Menu.FIRST + 2, 1, "删除信息"); }
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />