Advertisement
Guest User

Untitled

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