Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [Activity(Label = "Content Loader", MainLauncher = true, Icon = "@drawable/icon")]
- public class Activity1 : ListActivity, Android.Widget.SearchView.IOnQueryTextListener, LoaderManager.ILoaderCallbacks
- {
- // This is the Adapter being used to display the list's data.
- SimpleCursorAdapter mAdapter;
- // If non-null, this is the current filter the user has provided.
- String mCurFilter;
- protected override void OnCreate(Bundle bundle)
- {
- base.OnCreate(bundle);
- // Give some text to display if there is no data. In a real
- // application this would come from a resource.
- //setEmptyText("No phone numbers");
- // We have a menu item to show in action bar.
- //setHasOptionsMenu(true);
- // Create an empty adapter we will use to display the loaded data.
- mAdapter = new SimpleCursorAdapter(this,
- Android.Resource.Layout.SimpleListItem2, null,
- new String[] { Android.Provider.ContactsContract.ContactsColumns.DisplayName,
- Android.Provider.ContactsContract.ContactStatusColumns.ContactStatus },
- new int[] { Android.Resource.Id.Text1, Android.Resource.Id.Text2 }, 0);
- ListAdapter = mAdapter;
- // Prepare the loader. Either re-connect with an existing one,
- // or start a new one.
- LoaderManager.InitLoader(0, bundle, this);
- }
- public override bool OnCreateOptionsMenu(IMenu menu)
- {
- // Place an action bar item for searching.
- IMenuItem item = menu.Add("Search");
- item.SetIcon(Android.Resource.Drawable.IcMenuSearch);
- item.SetShowAsAction(Android.Views.ShowAsAction.IfRoom);
- SearchView sv = new SearchView(this);
- sv.SetOnQueryTextListener(this);
- item.SetActionView(sv);
- return base.OnCreateOptionsMenu(menu);
- }
- protected override void OnListItemClick(ListView l, View v, int position, long id) {
- // Insert desired behavior here.
- Android.Util.Log.Info("Content Loader Example", "Item clicked: " + id);
- }
- public CursorLoader OnCreateLoader(int id, Bundle args)
- {
- // This is called when a new Loader needs to be created. This
- // sample only has one Loader, so we don't care about the ID.
- // First, pick the base URI to use depending on whether we are
- // currently filtering.
- Android.Net.Uri baseUri;
- if (mCurFilter != null)
- {
- baseUri = Android.Net.Uri.WithAppendedPath(Contacts.People.ContentFilterUri,
- Android.Net.Uri.Encode(mCurFilter));
- }
- else
- {
- baseUri = Contacts.ContentUri;
- }
- // Now create and return a CursorLoader that will take care of
- // creating a Cursor for the data being displayed.
- String select = "((" + ContactsContract.ContactsColumns.DisplayName + " NOTNULL) AND ("
- + ContactsContract.ContactsColumns.HasPhoneNumber + "=1) AND ("
- + ContactsContract.ContactsColumns.DisplayName + " != '' ))";
- return new CursorLoader(this, baseUri,
- CONTACTS_SUMMARY_PROJECTION, select, null,
- ContactsContract.ContactsColumns.DisplayName + " COLLATE LOCALIZED ASC");
- }
- // These are the Contacts rows that we will retrieve.
- static String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
- //Contacts._ID,
- //Contacts.DISPLAY_NAME,
- //Contacts.CONTACT_STATUS,
- //Contacts.CONTACT_PRESENCE,
- //Contacts.PHOTO_ID,
- //Contacts.LOOKUP_KEY,
- Android.Provider.ContactsContract.Contacts.InterfaceConsts.Id,
- Android.Provider.ContactsContract.ContactsColumns.PhotoId,
- Android.Provider.ContactsContract.ContactsColumns.LookupKey,
- Android.Provider.ContactsContract.ContactsColumns.DisplayName,
- Android.Provider.ContactsContract.ContactStatusColumns.ContactStatus,
- Android.Provider.ContactsContract.StatusColumns.Status,
- Android.Provider.ContactsContract.StatusColumns.Presence,
- };
- public void OnLoadFinished(CursorLoader loader, Android.Database.ICursor data)
- {
- // Swap the new cursor in. (The framework will take care of closing the
- // old cursor once we return.)
- mAdapter.SwapCursor(data);
- }
- public void onLoaderReset(CursorLoader loader)
- {
- // This is called when the last Cursor provided to onLoadFinished()
- // above is about to be closed. We need to make sure we are no
- // longer using it.
- mAdapter.SwapCursor(null);
- }
- bool SearchView.IOnQueryTextListener.OnQueryTextChange(string newText)
- {
- mCurFilter = !String.IsNullOrEmpty(newText) ? newText : null;
- LoaderManager.RestartLoader(0, null, this);
- return true;
- }
- bool SearchView.IOnQueryTextListener.OnQueryTextSubmit(string query)
- {
- throw new NotImplementedException();
- }
- void IDisposable.Dispose()
- {
- //throw new NotImplementedException();
- }
- //IntPtr IJavaObject.Handle
- //{
- // get { throw new NotImplementedException(); }
- //}
- Loader LoaderManager.ILoaderCallbacks.OnCreateLoader(int id, Bundle args)
- {
- return LoaderManager.GetLoader(id);
- }
- public void OnLoadFinished(Loader loader, Java.Lang.Object data)
- {
- //throw new NotImplementedException();
- }
- public void OnLoaderReset(Loader loader)
- {
- //throw new NotImplementedException();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement