Advertisement
Guest User

Untitled

a guest
Jun 24th, 2012
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. using System;
  2.  
  3. using Android.App;
  4. using Android.Content;
  5. using Android.Runtime;
  6. using Android.Views;
  7. using Android.Widget;
  8. using Android.OS;
  9. using Android.Provider;
  10.  
  11. namespace ContentLoader
  12. {
  13. [Activity(Label = "Content Loader", MainLauncher = true, Icon = "@drawable/icon")]
  14. public class Activity1 : ListActivity, Android.Widget.SearchView.IOnQueryTextListener, LoaderManager.ILoaderCallbacks
  15. {
  16. // This is the Adapter being used to display the list's data.
  17. SimpleCursorAdapter mAdapter;
  18.  
  19. // If non-null, this is the current filter the user has provided.
  20. String mCurFilter;
  21.  
  22. protected override void OnCreate(Bundle bundle)
  23. {
  24. base.OnCreate(bundle);
  25.  
  26. // Give some text to display if there is no data. In a real
  27. // application this would come from a resource.
  28. //setEmptyText("No phone numbers");
  29.  
  30. // We have a menu item to show in action bar.
  31. //setHasOptionsMenu(true);
  32.  
  33.  
  34. // Create an empty adapter we will use to display the loaded data.
  35. mAdapter = new SimpleCursorAdapter(this,
  36. Android.Resource.Layout.SimpleListItem2, null,
  37. new String[] { Android.Provider.ContactsContract.ContactsColumns.DisplayName,
  38. Android.Provider.ContactsContract.ContactStatusColumns.ContactStatus },
  39. new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }, 0);
  40. ListAdapter = mAdapter;
  41. // Prepare the loader. Either re-connect with an existing one,
  42. // or start a new one.
  43. LoaderManager.InitLoader(0, bundle, this);
  44. }
  45.  
  46. public override bool OnCreateOptionsMenu(IMenu menu)
  47. {
  48. // Place an action bar item for searching.
  49.  
  50. IMenuItem item = menu.Add("Search");
  51. item.SetIcon(Android.Resource.Drawable.IcMenuSearch);
  52. item.SetShowAsAction(Android.Views.ShowAsAction.IfRoom);
  53. SearchView sv = new SearchView(this);
  54. sv.SetOnQueryTextListener(this);
  55. item.SetActionView(sv);
  56.  
  57. return base.OnCreateOptionsMenu(menu);
  58. }
  59.  
  60. protected override void OnListItemClick(ListView l, View v, int position, long id) {
  61. // Insert desired behavior here.
  62. Android.Util.Log.Info("Content Loader Example", "Item clicked: " + id);
  63. }
  64.  
  65. Loader LoaderManager.ILoaderCallbacks.OnCreateLoader(int id, Bundle args)
  66. {
  67. // This is called when a new Loader needs to be created. This
  68. // sample only has one Loader, so we don't care about the ID.
  69. // First, pick the base URI to use depending on whether we are
  70. // currently filtering.
  71. Android.Net.Uri baseUri;
  72. if (mCurFilter != null)
  73. {
  74. baseUri = Android.Net.Uri.WithAppendedPath(Contacts.People.ContentFilterUri,
  75. Android.Net.Uri.Encode(mCurFilter));
  76. }
  77. else
  78. {
  79. baseUri = Contacts.ContentUri;
  80. }
  81.  
  82. // Now create and return a CursorLoader that will take care of
  83. // creating a Cursor for the data being displayed.
  84. String select = "((" + ContactsContract.ContactsColumns.DisplayName + " NOTNULL) AND ("
  85. + ContactsContract.ContactsColumns.HasPhoneNumber + "=1) AND ("
  86. + ContactsContract.ContactsColumns.DisplayName + " != '' ))";
  87. return new CursorLoader(this, baseUri,
  88. CONTACTS_SUMMARY_PROJECTION, select, null,
  89. ContactsContract.ContactsColumns.DisplayName + " COLLATE LOCALIZED ASC");
  90. }
  91.  
  92. // These are the Contacts rows that we will retrieve.
  93. static String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
  94. //Contacts._ID,
  95. //Contacts.DISPLAY_NAME,
  96. //Contacts.CONTACT_STATUS,
  97. //Contacts.CONTACT_PRESENCE,
  98. //Contacts.PHOTO_ID,
  99. //Contacts.LOOKUP_KEY,
  100. Android.Provider.ContactsContract.Contacts.InterfaceConsts.Id,
  101. Android.Provider.ContactsContract.ContactsColumns.PhotoId,
  102. Android.Provider.ContactsContract.ContactsColumns.LookupKey,
  103. Android.Provider.ContactsContract.ContactsColumns.DisplayName,
  104. Android.Provider.ContactsContract.ContactStatusColumns.ContactStatus,
  105. Android.Provider.ContactsContract.StatusColumns.Status,
  106. Android.Provider.ContactsContract.StatusColumns.Presence,
  107. };
  108.  
  109. public void OnLoadFinished(CursorLoader loader, Android.Database.ICursor data)
  110. {
  111. // Swap the new cursor in. (The framework will take care of closing the
  112. // old cursor once we return.)
  113. mAdapter.SwapCursor(data);
  114. }
  115.  
  116. public void onLoaderReset(CursorLoader loader)
  117. {
  118. // This is called when the last Cursor provided to onLoadFinished()
  119. // above is about to be closed. We need to make sure we are no
  120. // longer using it.
  121. mAdapter.SwapCursor(null);
  122. }
  123.  
  124. bool SearchView.IOnQueryTextListener.OnQueryTextChange(string newText)
  125. {
  126. mCurFilter = !String.IsNullOrEmpty(newText) ? newText : null;
  127. LoaderManager.RestartLoader(0, null, this);
  128. return true;
  129. }
  130.  
  131. bool SearchView.IOnQueryTextListener.OnQueryTextSubmit(string query)
  132. {
  133. throw new NotImplementedException();
  134. }
  135.  
  136. void IDisposable.Dispose()
  137. {
  138. //throw new NotImplementedException();
  139. }
  140.  
  141. //IntPtr IJavaObject.Handle
  142. //{
  143. // get { throw new NotImplementedException(); }
  144. //}
  145.  
  146. //Loader LoaderManager.ILoaderCallbacks.OnCreateLoader(int id, Bundle args)
  147. //{
  148. // return LoaderManager.GetLoader(id);
  149. //}
  150.  
  151. public void OnLoadFinished(Loader loader, Java.Lang.Object data)
  152. {
  153. //throw new NotImplementedException();
  154. }
  155.  
  156. public void OnLoaderReset(Loader loader)
  157. {
  158. //throw new NotImplementedException();
  159. }
  160. }
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement