contentProvider
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:id="@+id/info" android:textSize="20px" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="联系人列表" /> <ListView android:id="@+id/linkManLists" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> =================contact.xml================= <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TableRow> <TextView android:id="@+id/_id" android:textSize="20px" android:layout_height="wrap_content" android:layout_width="60px" android:text="联系人列表" /> <TextView android:id="@+id/_name" android:textSize="20px" android:layout_height="wrap_content" android:layout_width="300px" android:text="联系人列表" /> </TableRow> </TableLayout> ==================================== package com.example.eightoperlinkmancontentprovider; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.provider.ContactsContract; import android.view.ContextMenu; 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; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class MainActivity extends Activity { private ListView contactsList=null; private Cursor result= null; private List<Map<String,Object>> allContactMap=null; //option +return private SimpleAdapter simpleAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.contactsList= (ListView) super.findViewById(R.id.linkManLists); this.result = super.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null); this.startManagingCursor(result); this.allContactMap = new ArrayList<Map<String, Object>>(); for (result.moveToFirst();!result.isAfterLast();result.moveToNext()){ Map<String, Object> contactMap = new HashMap<String, Object>(); contactMap.put("_id",result.getColumnIndex(ContactsContract.Contacts._ID)); contactMap.put("_name",result.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); this.allContactMap.add(contactMap); } this.simpleAdapter = new SimpleAdapter(this,allContactMap,R.layout.contacts,new String[]{"_id","_name"},new int[]{R.id._id,R.id._name}); this.contactsList.setAdapter(this.simpleAdapter); super.registerForContextMenu(this.contactsList);//reigister context menu } // @Override // public boolean onCreateOptionsMenu(Menu menu) { // // Inflate the menu; this adds items to the action bar if it is present. // getMenuInflater().inflate(R.menu.main, menu); // return true; // } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); menu.setHeaderTitle("联系人操作:"); menu.add(Menu.NONE, Menu.FIRST + 1, 1, "Show Detail"); menu.add(Menu.NONE,Menu.FIRST+2,1,"Delete Info"); } @Override public boolean onContextItemSelected(MenuItem item) {//选中某个菜单项 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); int idx = info.position; //取得数据ID long contactid=Long.parseLong(this.allContactMap.get(idx).get("_id").toString()); switch (item.getItemId()){ case Menu.FIRST+1: String phoneSelection = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=?"; //查询参数 String[] phoneSelectionArgs = {String.valueOf(contactid)}; //查询全部手机号码 Cursor c = super.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,phoneSelection,phoneSelectionArgs,null); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append("电话号码是:"); for (result.moveToFirst();!result.isAfterLast();result.moveToNext()){ stringBuffer.append(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))).append(","); } Toast.makeText(this,stringBuffer,Toast.LENGTH_LONG).show(); break; case Menu.FIRST+2: super.getContentResolver().delete(Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI,String.valueOf(contactid)),null,null); this.allContactMap.remove(idx);//删除数据项 this.simpleAdapter.notifyDataSetChanged(); Toast.makeText(this,"数据已经删除!",Toast.LENGTH_LONG).show(); break; } return false; } }