import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.database.Cursor;
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.TextView;
public class MainActivity extends Activity {
private ListView listView =null;
private SimpleAdapter simpleAdapter =null;
private List <Map<String ,Object>> all =null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView =(ListView)super.findViewById(R.id.listview);
Cursor result = super.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
super.startManagingCursor(result);
//交给系统处理
all =new ArrayList <Map<String ,Object>>();
for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){
Map<String,Object> map =new HashMap<String ,Object>();
map.put("id", result.getInt(result.getColumnIndex(ContactsContract.Contacts._ID)));
map.put("name", result.getString(result.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
all.add(map);
}
//设置simpleAdapter
simpleAdapter=new SimpleAdapter
(this,all,
R.layout.simple,
new String[]{"name","id"},
new int[]{R.id.name,R.id.id});
listView.setAdapter(simpleAdapter);
super.registerForContextMenu(listView);
//注册上下文菜单
; }
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info =(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
int position =info.position;
//取得menu的位置,此位置与adapter中的位置一致的位置一致
String posId =this.all.get(position).get("id").toString();
//得到list集合的position项键为id的 值
String phoneSelection =ContactsContract.CommonDataKinds.Phone._ID+"=?";
String phoneArgs [] = new String []{posId};
switch (item.getItemId()){
case Menu.FIRST+1:
//查看详情
Cursor c =super.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, phoneSelection,
phoneArgs, null);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
Dialog dialog = new AlertDialog.Builder(this).
setTitle("电话信息").
setMessage(c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)))
.create();
dialog.show();
}
break;
case Menu.FIRST+2:
if(phoneSelection != null&& !"".equals(phoneSelection)){
int c1 =super.getContentResolver().delete(Uri.withAppendedPath(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
posId),
phoneSelection,
phoneArgs);
all.remove(position);
simpleAdapter.notifyDataSetChanged();
}
}
return false;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
menu.add(Menu.NONE, Menu.FIRST+1,1, "查看具体信息");
menu.add(Menu.NONE, Menu.FIRST+2,2, "删除");
super.onCreateContextMenu(menu, v, menuInfo);
}
@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;
}
}
//权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contentprovider"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.contentprovider.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
</manifest>