当前位置:操作系统 > 安卓/Android >>

使用系统定义的Content Provider

之前我们介绍了关于Content Provider 的内容。这里我们在分两节来介绍,使用系统定义的Content Provider和自定义的Content Provider。

    这里我们来介绍使用系统定义的Content Provider:通过ContentResolver来读取系统的联系人信息。

   第一步: 新建一个工程命名为readPeople,因为我们以列表的形式来显示,所以这个类继承自ListAvtivity而不是Activity;

   第二步:修改java源码,代码如下:


view plaincopy to clipboardprint?/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    //setContentView(R.layout.main);  
    //获得游标对象  
    Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    this.startManagingCursor(c); 
     
    //实例化列表适配器  
stAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[] {Contacts.DISPLAY_NAME}, new int[] {android.R.id.text1}); 
    this.setListAdapter(adapter); 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        //获得游标对象
        Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
        this.startManagingCursor(c);
       
        //实例化列表适配器
  ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, c, new String[] {Contacts.DISPLAY_NAME}, new int[] {android.R.id.text1});
        this.setListAdapter(adapter);
    }view plaincopy to clipboardprint?第三步:在AndroidManifestxml文件中添加如下内容:<uses-permission android:name="android.permission.READ_CONTACTS"/> 
  第三步:在AndroidManifestxml文件中添加如下内容:<uses-permission android:name="android.permission.READ_CONTACTS"/>   第四步:运行程序可看到如下内容:


\

摘自 chenlong12580的专栏
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,