用SimpleCursorAdapter来获取手机联系人

package com.dhm;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Color;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
//listView
public class D_02 extends Activity{
    //创建listview和布局实例
    //通过listAdapter将获得的电话本数据与listview连接起来
    //将listview添加到布局中
private ListView listView;
private LinearLayout linearLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    linearLayout = new LinearLayout(this);
    //设置布局属性
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setBackgroundColor(Color.RED);
    //创建listview对象
    listView = new ListView(this);
    linearLayout.setBackgroundColor(Color.BLACK);
//    LayoutParams继承于Android.View.ViewGroup.LayoutParams.
//    在Java中动态构建的布局,常常这样写:(第一个参数是宽,第二个参数是高)
//    setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    linearLayout.addView(listView, layoutParams);//添加到布局中取
    //设置显示布局
    setContentView(linearLayout);
    //因为是电话本,所以要获取数据库
//    Cursor 是每行的集合。
//    使用 moveToFirst() 定位第一行。
//    你必须知道每一列的名称。
//    你必须知道每一列的数据类型。
//    Cursor 是一个随机的数据源。
//    所有的数据都是通过下标取得。
//    (1) 为空的Cursor的判断
//    if (cur.moveToFirst() == false)
//    {
//    //为空的Cursor
//    return;
//    }
//    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
//    Cursor cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, null, null, null);
    Cursor cursor =getContentResolver().query(People.CONTENT_URI, null, null, null, null);
    startManagingCursor(cursor);
    
//    在使用数据库操作查询数据后,如果是在Activity里面处理,那么很可能就会用到startManagingCursor()方法,
//    1.这个方法使用的前提是:游标结果集里有数据记录。所以,在使用之前,先对Cursor是否为null进行判断,如果Cursor != null,再使用此方法
//            2.如果使用这个方法,最后也要用stopManagingCursor()来把它停止掉,以免出现错误。
//            3.使用这个方法的目的是把获取的Cursor对象交给Activity管理,这样Cursor的生命周期便能和Activity自动同步,省去自己手动管理。如果不调用,那么
    //==============================================
//query(Uri, String[], String, String[], String)
//insert(Uri, ContentValues)
//update(Uri, ContentValues, String, String[])
//delete(Uri, String, String[])
//    Android是如何实现应用程序之间数据共享的?
//一个应用程序可以将自己的数据完全暴露出去,外界更本看不到,也不用看到这个应用程序暴露的数据是如何存储的,
//    或者是使用数据库还是使用文件,还是通过网上获得,这些一切都不重要,重要的是外界可以通过这一套标准及统一的接口和这个程序里的数据打交道,
//    例如:添加(insert)、删除(delete)、查询(query)、修改(update),当然需要一定的权限才可以。
//========================================================
//Android提供了ContentProvider,一个程序可以通过实现一个Content provider的抽象接口将自己的数据完全暴露出去,
//而且Content providers是以类似数据库中表的方式将数据暴露。Content providers存储和检索数据,通过它可以让所有的应用程序访问到,
//这也是应用程序之间唯一共享数据的方法。要想使应用程序的数据公开化,
//可通过2种方法:创建一个属于你自己的Content provider或者将你的数据添加到一个已经存在的Content provider中,
//前提是有相同数据类型并且有写入Content pr
//后一个问题:如何获取ContentResolver?调用getContentResolver (),例如:ContentResolver cr = getContentResolver();
    //接下来就是为listview绑定listAdapter
//    Adapter让它显示出来,
//    SimpleCursorAdapter则可以从数据库中读取数据显示在列表上
    ListAdapter listAdapter = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_2,cursor, new String[]{PhoneLookup.DISPLAY_NAME,PhoneLookup.NUMBER}, new int[]{android.R.id.text1,android.R.id.text2});
    //第一个参数表示当前对象
    //第二参数是每一行数据包含两个数据
    //第三个参数是添加数据库
    //第四个是从名字和电话获取数据
    //第五个是是显示电话和名字
    listView.setAdapter(listAdapter);
    //接下来是为listview设置监听
//    当点击了第几个选项产生事件
    listView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            DisplayToast("滚动到了"+Long.toString(arg0.getSelectedItemId())+"项");
        }

        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
//            DisplayToast("滚动到了"+Integer.toString(arg2+1)+"项");
        }
        
    });
    //listview的点击事件
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            DisplayToast("用户点击了第几项"+Integer.toString(arg2+1));
            
        }
    });
}

//显示的方法
public void DisplayToast(String str){
    Toast.makeText(this, str,Toast.LENGTH_SHORT).show();
}
}
//最后要在manifest中配置
//<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
//而且她要放在application前面,不然会保错

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值