Posted By

variable3 on 05/04/11


Tagged


Versions (?)

code for retriving contacts using content provider


 / Published in: Java
 

  1. // MainActivity.java
  2.  
  3. package com.v3.contact;
  4.  
  5.  
  6. import android.app.ListActivity;
  7. import android.content.ContentValues;
  8. import android.content.Intent;
  9. import android.database.Cursor;
  10. import android.net.Uri;
  11. import android.os.Bundle;
  12. import android.provider.ContactsContract;
  13. import android.provider.ContactsContract.CommonDataKinds.Phone;
  14. import android.provider.ContactsContract.Data;
  15. import android.view.View;
  16. import android.widget.ListAdapter;
  17. import android.widget.ListView;
  18. import android.widget.SimpleCursorAdapter;
  19. import android.widget.TextView;
  20.  
  21. public class MainActivity extends ListActivity {
  22.  
  23. TextView ContactsTV;
  24. ListView ContactsLV;
  25. public static final Uri CONTENT_URI = Uri
  26. .parse("content://com.android.contacts/contacts/1557");
  27.  
  28. /** Called when the activity is first created. */
  29. @Override
  30. public void onCreate(Bundle savedInstanceState) {
  31. super.onCreate(savedInstanceState);
  32. setContentView(R.layout.main);
  33.  
  34.  
  35. Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
  36.  
  37.  
  38. Cursor c = getContentResolver().query(myContacts, new String[]
  39. {ContactsContract.CommonDataKinds.Phone._ID,
  40. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  41. ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Email.DISPLAY_NAME}, null, null, null);
  42. String[] columns = new String[] {
  43. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  44. ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Email.DISPLAY_NAME};
  45.  
  46. // the XML defined views which the data will be bound to
  47. int[] to = new int[] {R.id.text1,R.id.text2,R.id.text3};
  48.  
  49. SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
  50. R.layout.listitems, c, columns, to);
  51.  
  52. // set this adapter as your ListActivity's adapter
  53. setListAdapter(mAdapter);
  54.  
  55. }
  56.  
  57.  
  58.  
  59.  
  60. }
  61.  
  62. //listsitems.xml
  63.  
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <LinearLayout
  66. xmlns:android="http://schemas.android.com/apk/res/android"
  67. android:layout_width="match_parent"
  68. android:layout_height="match_parent" android:orientation="vertical">
  69.  
  70. <TextView android:text="" android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
  71. <TextView android:text="" android:id="@+id/text2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
  72. <TextView android:text="" android:id="@+id/text3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
  73.  
  74. </LinearLayout>
  75. //main.xml
  76. <?xml version="1.0" encoding="utf-8"?>
  77. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  78. android:orientation="vertical"
  79. android:layout_width="fill_parent"
  80. android:layout_height="fill_parent"
  81. >
  82.  
  83. <ListView android:layout_height="wrap_content" android:id="@+id/android:list" android:layout_width="match_parent"></ListView>
  84. </LinearLayout>
  85. //menifest.xml
  86. <?xml version="1.0" encoding="utf-8"?>
  87. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  88. package="com.v3.contact"
  89. android:versionCode="1"
  90. android:versionName="1.0">
  91.  
  92.  
  93. <uses-sdk android:minSdkVersion="8" />
  94. <uses-permission android:name="android.permission.READ_CONTACTS" />
  95.  
  96.  
  97. <application android:icon="@drawable/icon" android:label="@string/app_name">
  98. <activity android:name=".MainActivity"
  99. android:label="@string/app_name">
  100. <intent-filter>
  101. <action android:name="android.intent.action.MAIN" />
  102. <category android:name="android.intent.category.LAUNCHER" />
  103. </intent-filter>
  104. </activity>
  105.  
  106. </application>
  107. <provider android:name=".Contacts"
  108. android:authorities="com.android.contacts" />
  109. </manifest>

Report this snippet  

You need to login to post a comment.