界面配置文件

<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/text"
        android:textSize="30px"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="手机联系人列表"/>
    <ListView 
        android:id="@+id/contactslist"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

SimpleAdapter的配置文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >
    <TextView
        android:id="@+id/_id"
        android:textSize="30px"
        android:layout_width="240px"
        android:layout_height="wrap_content"/>
<TextView
        android:id="@+id/name"
        android:textSize="30px"
        android:layout_width="240px"
        android:layout_height="wrap_content"/>
</LinearLayout>

Acitivity程序

package com.example.contactproject;
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.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.Toast;
public class MainActivity extends Activity {
private ListView contactslist=null;
private SimpleAdapter simpleadapter=null;
private Cursor result=null;
private List<Map<String, Object>> allContacts=new ArrayList<Map<String,Object>>();
@Override
public boolean onContextItemSelected(MenuItem item) {//选中某个菜单项
AdapterView.AdapterContextMenuInfo info=(AdapterView.AdapterContextMenuInfo)
item.getMenuInfo();//取得菜单项
int position=info.position;//取得操作ID
String contactsId=this.allContacts.get(position).get("_id").toString();//取得数据ID
switch(item.getItemId()){//判断菜单项ID
case Menu.FIRST+1://查询数据
String phoneSelection=ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"=?";//设置查询条件
   String[] phoneSelectionAras=new String[]{contactsId}; //查询参数
   Cursor result=super.getContentResolver().query(
   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
   null, phoneSelection, phoneSelectionAras, null);//获得查询的手机号码
   StringBuffer buf=new StringBuffer();//用于接受号码
   buf.append("电话号码是:"); 
   for(result.moveToFirst();!result.isAfterLast();result.moveToNext()){//循环取出数据
   buf.append(result.getString(result.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER))+"\\");
   }
   Toast.makeText(this, buf.toString(), Toast.LENGTH_LONG).show();
break;
case Menu.FIRST+2://删除数据 
super.getContentResolver().delete(Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_URI,
contactsId), 
null, null);//删除指定数据
   this.allContacts.remove(position);//删除数据项
   this.simpleadapter.notifyDataSetChanged();//更新列表项
   Toast.makeText(this, "数据已经删除", Toast.LENGTH_LONG).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, 2, "删除联系人");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.activity_main);
this.contactslist=(ListView)super.findViewById(R.id.contactslist);
this.result=super.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
super.startManagingCursor(result);
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)));
this.allContacts.add(map);
}
this.simpleadapter=new SimpleAdapter(
this, this.allContacts,
R.layout.contacts,
new String[]{"_id","name"},
new int[]{R.id._id,R.id.name});
this.contactslist.setAdapter(this.simpleadapter);
this.registerForContextMenu(this.contactslist);
}
@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.contactproject"
    android:versionCode="1"
    android:versionName="1.0" >

<uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="10" />

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.contactproject.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>

版权声明:本文为博主原创文章,未经博主允许不得转载。

android操作通讯录的联系人的更多相关文章

  1. Android向通讯录添加联系人的一般方法

    Android向通讯录添加联系人的一般方法 以一个简单的例子加以说明,记得需要相应的权限: 测试代码,关键的内容就在add函数里面. package zhangphil.demo; import an ...

  2. Android项目——读取手机联系人信息

    加入读取联系人信息的权限 <uses-permission android:name="android.permission.READ_CONTACTS"/> cont ...

  3. Android实现SQLite数据库联系人列表

    Android实现SQLite数据库联系人列表 开发工具:Andorid Studio 1.3 运行环境:Android 4.4 KitKat 工程内容 实现一个通讯录查看程序: 要求使用SQLite ...

  4. Android开发之读写联系人

    读写联系人需要用到android的ContentProvider 同时需要读和写联系人的权限 需要使用到联系人数据库中的 * raw_contacts表: * contact_id:联系人id * d ...

  5. Android操作HTTP实现与服务器通信(转)

    Android操作HTTP实现与服务器通信   本示例以Servlet为例,演示Android与Servlet的通信. 众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方 ...

  6. android操作sdcard中的多媒体文件(二)——音乐列表的更新

    android操作sdcard中的多媒体文件(二)——音乐列表的更新 原文地址 在上一篇随笔中,我介绍了如何在程序中查询sdcard内的多媒体文件,并且显示到播放列表中,但是,如果在sdcard内删除 ...

  7. android操作sdcard中的多媒体文件(一)——音乐列表的制作

    android操作sdcard中的多媒体文件(一)——音乐列表的制作 原文地址 最近做了一个android音乐播放器,个人感觉最难的就是“后台播放”以及有关“播放列表”的部分,但是总算是找到了实现的方 ...

  8. [android] 获取系统的联系人信息

    内容提供是实质上是个接口,后门,他给别人提供数据,系统联系人是个比较复杂的内容通过者. 找到/data/data/com.android.providers.contacts/contacts2.db ...

  9. android操作sqlite数据库及心得

    写这篇文章主要是网上的对sqlite的操作太多且太杂,非常多时候都不能非常好的运用到自己的项目中,结构不清晰,我自己写了一篇适合刚刚接触的人看的操作方法. 近来用android时要将一些数据保存起来, ...

随机推荐

  1. 如何在Visual Studio中选择C++和C#的编译器版本

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:如何在Visual Studio中选择C++和C#的编译器版本.

  2. python的locals()妙用

    如果你是个喜欢偷懒的程序员并想让代码看起来更加简明,可以利用 Python 的内建函数 locals() .它返回的字典对所有局部变量的名称与值进行映射.因此,前面的视图可以重写成下面这个样子:def ...

  3. Linux在向磁盘新建文件的时候在系统层面的四步操作

    ----------- 1.存储文件属性.内核先找到一个空的i节点,并把文件属性信息记录其中.   2.存储数据.先计算要多少个磁盘块,在内核自由块列表找到合适数量的磁盘块,并把数据从内核的缓冲区依次 ...

  4. 配置DNS服务器IP

    #############################脚本功能及说明#################### #该脚本用来在本地服务器上配置DNS服务器IP #创建时间:2014-10-22 ## ...

  5. SPOJ LIS2 Another Longest Increasing Subsequence Problem 三维偏序最长链 CDQ分治

    Another Longest Increasing Subsequence Problem Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://a ...

  6. Codeforces Round #185 (Div. 2) B. Archer 水题

    B. Archer Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/312/problem/B D ...

  7. Codeforces Round #331 (Div. 2) D. Wilbur and Trees 记忆化搜索

    D. Wilbur and Trees Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  8. 内核映像的形成 —— KBuild体系

    1. http://blog.csdn.net/yunsongice/article/details/6046537 2. http://blog.csdn.net/yunsongice/articl ...

  9. aspose.cells 模版

    aspose.cells 模版  http://www.cnblogs.com/whitewolf/archive/2011/03/21/Aspose_Cells_Template1.html

  10. null和空 not null

    所谓的NULL就是什么都没有,连\0都没有,\0在字符串中是结束符,但是在物理内存是占空间的,等于一个字节,而NULL就是连这一个字节都没有.在 数据库里是严格区分的,任何数跟NULL进行运算都是NU ...