Posted By

variable3 on 05/04/11


Tagged

show content read a in for IT provider android contacts listview


Versions (?)

Android Conent provider


 / Published in: Java
 

  1. public class MainActivity extends ListActivity {
  2. //Declare CONTENT_URI for contacts
  3. public static final Uri CONTENT_URI = Uri.parse("content://com.android.contacts/contacts/1557");
  4. public void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.main);
  7.  
  8. Uri myContacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
  9.  
  10. Cursor c = getContentResolver().query(myContacts, new String[]
  11. {ContactsContract.CommonDataKinds.Phone._ID,
  12. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  13. ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Email.DISPLAY_NAME}, null, null, null);
  14. String[] columns = new String[] {
  15. ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
  16. ContactsContract.CommonDataKinds.Phone.NUMBER,ContactsContract.CommonDataKinds.Email.DISPLAY_NAME};
  17.  
  18. // the XML defined views which the data will be bound to
  19. int[] to = new int[] { R.id.name_entry, R.id.number_entry,R.id.email_entry };
  20.  
  21. SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
  22. R.layout.contacts, c, columns, to);
  23.  
  24. // set this adapter as your ListActivity's adapter
  25. setListAdapter(mAdapter);
  26. }
  27. }
  28.  
  29. main.xml
  30. ---------
  31. <?xml version="1.0" encoding="utf-8"?>
  32. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  33. android:orientation="vertical" android:layout_width="fill_parent"
  34. android:layout_height="fill_parent">
  35. <ListView android:id="@android:id/android:list"
  36. android:layout_width="fill_parent" android:layout_height="wrap_content"
  37. android:background="#00FF00" />
  38. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  39. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  40. android:orientation="vertical"
  41. android:layout_width="fill_parent"
  42. android:layout_height="fill_parent"
  43. >
  44. <TextView
  45. android:id="@+id/name_entry"
  46. android:layout_width="wrap_content"
  47. android:layout_height="wrap_content"
  48. android:textSize="28dip" />
  49. <TextView
  50. android:id="@+id/number_entry"
  51. android:layout_width="wrap_content"
  52. android:layout_height="wrap_content"
  53. android:textSize="28dip" />
  54. <TextView
  55. android:id="@+id/email_entry"
  56. android:layout_width="wrap_content"
  57. android:layout_height="wrap_content"
  58. android:textSize="28dip" />
  59. </LinearLayout>
  60.  
  61.  
  62. contacts.xml
  63. ------------
  64. <?xml version="1.0" encoding="utf-8"?>
  65. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  66. android:orientation="vertical"
  67. android:layout_width="fill_parent"
  68. android:layout_height="fill_parent"
  69. >
  70. <TextView
  71. android:id="@+id/name_entry"
  72. android:layout_width="wrap_content"
  73. android:layout_height="wrap_content"
  74. android:textSize="28dip" />
  75. <TextView
  76. android:id="@+id/number_entry"
  77. android:layout_width="wrap_content"
  78. android:layout_height="wrap_content"
  79. android:textSize="28dip" />
  80. <TextView
  81. android:id="@+id/email_entry"
  82. android:layout_width="wrap_content"<?xml version="1.0" encoding="utf-8"?>
  83. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  84. android:orientation="vertical"
  85. android:layout_width="fill_parent"
  86. android:layout_height="fill_parent"
  87. >
  88. <TextView
  89. android:id="@+id/name_entry"
  90. android:layout_width="wrap_content"
  91. android:layout_height="wrap_content"
  92. android:textSize="28dip" />
  93. <TextView
  94. android:id="@+id/number_entry"
  95. android:layout_width="wrap_content"
  96. android:layout_height="wrap_content"
  97. android:textSize="28dip" />
  98. <TextView
  99. android:id="@+id/email_entry"
  100. android:layout_width="wrap_content"
  101. android:layout_height="wrap_content"
  102. android:textSize="28dip" />
  103. </LinearLayout>
  104.  
  105. android:layout_height="wrap_content"
  106. android:textSize="28dip" />
  107. </LinearLayout>
  108.  
  109.  
  110. AndroidMainifest.xml
  111. --------------------
  112. <?xml version="1.0" encoding="utf-8"?>
  113. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  114. package="com.v3.ContProv"
  115. android:versionCode="1"
  116. android:versionName="1.0">
  117. <uses-permission android:name="android.permission.READ_CONTACTS"/>
  118. <uses-sdk android:minSdkVersion="8" />
  119. <permission android:name="com.v3.ContProv" />
  120.  
  121.  
  122. <application android:icon="@drawable/icon" android:label="@string/app_name">
  123. <activity android:name=".MainActivity"
  124. android:label="@string/app_name">
  125. <intent-filter>
  126. <action android:name="android.intent.action.MAIN" />
  127. <category android:name="android.intent.category.LAUNCHER" />
  128. </intent-filter>
  129. </activity>
  130.  
  131. </application>
  132. <provider android:name=".Contacts"
  133. android:authorities="com.android.contacts" />
  134. </manifest>

Report this snippet  

You need to login to post a comment.