// Handle the dismiss event on the dialog to raise a custom event that passes the updated order fields back
        public override void OnDismiss(IDialogInterface dialog)
        {
            base.OnDismiss(dialog);

            // Verify the event has listeners
            if (OrderFieldDialogClosed != null)
            {
                // Get an array of checked list item positions (indices)
                SparseBooleanArray checkedItemsArray = _orderFieldsListView.CheckedItemPositions;

                // Loop through all the available order fields
                for (int i = 0; i < _potentialOrderByFields.Count; i++)
                {
                    // Initially set each order option to false
                    OrderFieldOption orderOption = _potentialOrderByFields[i];
                    orderOption.OrderWith = false;

                    // If the item was checked in the list view, set the order option to true
                    if (checkedItemsArray.KeyAt(i) == i && checkedItemsArray.ValueAt(i))
                    {
                        orderOption.OrderWith = true;
                    }
                }

                // Raise the event and pass back the updated list of order field options
                OrderFieldDialogClosed(this, _potentialOrderByFields);
            }
        }
        private void ShowGroupFields(object sender, EventArgs e)
        {
            // Create a dialog for choosing fields to group results with
            GroupFieldsDialog groupFieldsDialog = new GroupFieldsDialog(_groupByFields);

            // Handle the dialog closing event to read the selected fields
            groupFieldsDialog.GroupFieldDialogClosed += (s, args) =>
            {
                // Update the dictionary of group fields from the dialog
                _groupByFields = args;

                // Get the current list of group fields and create/update the sort field choices
                // (only fields selected for grouping can be used to order results)
                List <KeyValuePair <string, bool> > currentGroupFields = _groupByFields.Where(field => field.Value == true).ToList();

                // Loop through the group fields
                foreach (KeyValuePair <string, bool> groupField in currentGroupFields)
                {
                    // Check if this field is missing from the current sort field options
                    OrderFieldOption existingOption = _orderByFields.Find((opt) => opt.OrderInfo.FieldName == groupField.Key);
                    if (existingOption == null)
                    {
                        // If the field is missing, create a new OrderFieldOption and add it to the list
                        existingOption = new OrderFieldOption(false, new OrderBy(groupField.Key, SortOrder.Ascending));
                        _orderByFields.Add(existingOption);
                    }
                }

                // Also make sure to remove any 'order by' fields that were removed from the 'group by' list
                for (int i = _orderByFields.Count - 1; i >= 0; i--)
                {
                    // If the order by field is not also one of the group fields, remove it from the list
                    OrderFieldOption            opt = _orderByFields.ElementAt(i);
                    KeyValuePair <string, bool> existingGroupField = currentGroupFields.FirstOrDefault(field => field.Key == opt.OrderInfo.FieldName);
                    if (existingGroupField.Key == null)
                    {
                        _orderByFields.RemoveAt(i);
                    }
                }
            };

            // Begin a transaction to show a UI fragment (group fields dialog)
            FragmentTransaction trans = FragmentManager.BeginTransaction();

            groupFieldsDialog.Show(trans, "group_flds");
        }