<!-- 读取联系人记录的权限 -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
activity_main<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"
    tools:context="${relativePackage}.${activityClass}" >

    <EditText
        android:id="@+id/et_main_number"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入或选择一个联系人号码" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/et_main_number"
        android:text="选择联系人"
        android:onClick="toContactList"/>

</RelativeLayout>
item_contact<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="5dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/tv_item_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp"/>

    <TextView
        android:id="@+id/tv_item_number"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:textSize="18sp"
        android:layout_marginLeft="10dp"/>

</LinearLayout>
activity_contact_list<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</ListView>
public class MainActivity extends Activity {

    private EditText et_main_number;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        et_main_number = (EditText) findViewById(R.id.et_main_number);
    }

    public void toContactList(View v) {
        //启动联系人列表界面
        startActivityForResult(new Intent(this, ContactListActivity.class), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1 && resultCode==RESULT_OK) {
            //得到返回的number
            String number = data.getStringExtra("NUMBER");
            //显示
            et_main_number.setText(number);
        }
    }
}
public class ContactListActivity extends ListActivity implements OnItemClickListener {

    private ListView listView;
    private ContactAdapter adapter;
    private List<Map<String, String>> data = new ArrayList<Map<String,String>>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_list);

        listView = getListView();
        adapter = new ContactAdapter();
        //查询得到联系人表数据
            //ContentResolver
        ContentResolver resolver = getContentResolver();
            //执行查询得到cursor
        String[] projection = {Phone.DISPLAY_NAME, Phone.NUMBER};
        Cursor cursor = resolver.query(Phone.CONTENT_URI, projection, null, null, null);
            //取出其中的数据保存到data
        while(cursor.moveToNext()) {
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            Map<String, String> map = new HashMap<String, String>();
            map.put("name", name);
            map.put("number", number);
            data.add(map);
        }
        //显示列表
        listView.setAdapter(adapter);

        //给listView添加item点击监听
        listView.setOnItemClickListener(this);

    }

    class ContactAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if(convertView==null) {
                convertView = View.inflate(ContactListActivity.this, R.layout.item_contact, null);
            }

            Map<String, String> map = data.get(position);
            TextView nameTV = (TextView) convertView.findViewById(R.id.tv_item_name);
            TextView nubmerTV = (TextView) convertView.findViewById(R.id.tv_item_number);
            nameTV.setText(map.get("name"));
            nubmerTV.setText(map.get("number"));

            return convertView;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        //得到选择的号码
        String number = data.get(position).get("number");
        Intent intent = getIntent();
        intent.putExtra("NUMBER", number);
        //设置结果
        setResult(RESULT_OK, intent);
        //返回
        finish();
    }
}

查询所有联系人并选中显示 contentprovider的更多相关文章

  1. Android 手机卫士--获取联系人信息并显示与回显

    前面的文章已经实现相关的布局,本文接着进行相关的功能实现 本文地址:http://www.cnblogs.com/wuyudong/p/5951794.html,转载请注明出处. 读取系统联系人 当点 ...

  2. 使用实时文件夹显示ContentProvider的数据

    所谓实时文件夹(即LiveFolder),是指用于显示ContentProvider提供的数据的桌面组件. ContentProvider用于向外提供数据访问的接口,一个应用程序可通过ContentP ...

  3. js控制select选中显示不同表单内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 分页查询关键代码 多条件查询关键代码 删除选中商品关键代码 修改要先回显再修改 修改要先回显再修改 同一业务集中使用同一servlet的方法

    分页查询关键代码: 通过servlet转发回来的各种信息进行分页的设计(转发回的信息有 分页查询的List集合 查询的页码 查询的条数 查询的数据库总条数 查询的总页码) 从开始时循环10次出现十个数 ...

  5. PHP--TP框架----把查询到的数据,显示在模型(模板)里面

    MainController.class.php <?php namespace Home\Controller; use Think\Controller; class MainControl ...

  6. jsp多条件查询及查询结果在同一页面显示(原创)

    第一步,建立main.jsp页面,使用frameset分上下两个框架,上部是query.jsp.下部是detail .detail显示的是showdetail.jsp的页面 <title> ...

  7. Saiku对Measure(指标)查询结果进行计算后显示的方法

    在通过Saiku查询数据的时候,当需要改变查询结果的显示方式的时候,可以添加formatString属性,但是当需要计算查询结果的时候,则需要使用CellFormatter,使用方法如下: <M ...

  8. 通过RF数据库查询中文字段结果正常显示的转换方法

    方法1:统一显示为中文 1.通过RF数据库查询中文字段结果格式:'\xba\xcb\xbc\xf5\xcd\xa8\xb9\xfd' 2.通过Decode Bytes To String进行gbk解码 ...

  9. MySql查询数据令某字段显示固定值

    我们用SQL查询数据时后,基于某些原因不想看到某字段的值,比如密码,我们可以通过创建视图,忽略某一字段的值. 同时我们也可以直接通过SQL语句来让其显示某个固定值: (1)一般查询语句: SELECT ...

随机推荐

  1. 图片点击放大并可点击旋转插件(1)-jquery.artZoom.js

    1.首先加入链接: <script type="text/javascript" src="js/jquery-1.6.1.min.js">< ...

  2. spring mvc利用MultipartResolver解析Multipart/form-data进行文件上传

    之前的表单数据都是文本数据,现记录:利用MultipartResolver进行文件上传. ①首先,需引入commons-fileUpload和commons-io jar包,pom.xml文件的坐标: ...

  3. java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.github.pagehelper.Page pagehelper报错无法类型转化。

    报错信息: 严重: Servlet.service() for servlet [springmvc] in context with path [] threw exception [Request ...

  4. Geoserver跨域请求设置

    使用OpenLayers请求GeoServer发布的WFS服务时,如果不是相同的域可能会出现如下问题. 已拦截跨源请求:同源策略禁止读取位于 http://localhost:8080/geoserv ...

  5. python 工具 图片批量合并

    注:代码两处设置 region = (4,3,x-3,y-5) 目的是crop剪去图片的白边,这个可以视情况改变 图片需要命名为   x_1.png   .....这样的格式 #encoding=ut ...

  6. MySQL主从复制技术与读写分离技术amoeba应用

    MySQL主从复制技术与读写分离技术amoeba应用 前言:眼下在搭建一个人才站点,估计流量会非常大,须要用到分布式数据库技术,MySQL的主从复制+读写分离技术.读写分离技术有官方的MySQL-pr ...

  7. 百科知识 hta文件如何打开

    后缀名为hta是什么文件,谢谢? 2006-10-11 21:36 提问者: tanhailong2006 | 浏览次数:2092次 我来帮他解答 输入内容已经达到长度限制 还能输入 9999 字 插 ...

  8. Flash中如何使用滤镜

    使用滤镜 应用或删除滤镜 复制和粘贴滤镜 为对象应用预设滤镜 启用或禁用应用于对象的滤镜 启用或禁用应用于对象的所有滤镜 创建预设滤镜库 对象每添加一个新的滤镜,在属性检查器中,就会将其添加到该对象所 ...

  9. nagios插件之登陆SBC监控电话数

    运行:sbc_calls_status_new auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh vi sbc_calls_status_new.c #inc ...

  10. HDU 3435A new Graph Game(网络流之最小费用流)

    题目地址:HDU 3435 这题刚上来一看,感觉毫无头绪. .再细致想想.. 发现跟我做的前两道费用流的题是差点儿相同的. 能够往那上面转换. 建图基本差点儿相同.仅仅只是这里是无向图.建图依旧是拆点 ...