0

I pretty stumped on how to refresh a ListAdapter I have in an activity from an AlertDialog I'm calling from the same activity.

Here's the code of the activity:

private static ArrayAdapter<CarProfile> mainListAdapter;

public class CarProfiles : ListActivity
{
  protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        mainListAdapter = new ArrayAdapter<CarProfile>(this, Android.Resource.Layout.SimpleListItem1, carProfiles);
        // This targets a ListView in my axml with id list.
        ListAdapter = mainListAdapter;

        ShowCarProfileFormDialog(parameters blah, blah, blah);
    }
}

And this is my AlertDialog:

    public class CarProfileDialogFragment : DialogFragment
    {
        public override Dialog OnCreateDialog(Bundle savedInstanceState)
        {
            LayoutInflater inflater = Activity.LayoutInflater;
            View view = inflater.Inflate(Resource.Layout.CarProfileForm, null);

            // component init (removed)

            var builder = new AlertDialog.Builder(Activity)
                .SetView(view)
                .SetPositiveButton(GetString(Resource.String.lblCarProfileDialogOK), (sender, args) =>
                {        
                    // The datasouce source update works
                    datasource.UpdateCarProfile(id, txtName.Text, txtPlateNumber.Text, spnCategoryColor.SelectedItem.ToString(), spnCategoryNumber.SelectedItem.ToString());

                    // But this doesn't 
                    mainListAdapter.NotifyDataSetChanged();                        
                })
                .SetNegativeButton(GetString(Resource.String.lblCarProfileDialogCancel), (sender, args) =>
                {
                    Dialog.Dismiss();
                })
                .SetTitle(GetString(Resource.String.lblCarProfileDialogTitle));
            return builder.Create();
        }
    }

Shown in the AlertDialog code above, my datasouce get updated with no problems and when I call the NotifyDataSetChanged method nothing happens.

Ron
  • 1,721
  • 1
  • 19
  • 43
  • 1
    Does that ever compile ? There are a couple of java synxtax errors in this file. – Snicolas Dec 12 '12 at 07:29
  • 1
    This is monodroid. I don't mind a Java solution though. – Ron Dec 12 '12 at 07:30
  • I can't say for MonoDroid, but in normal Java you should use an event based mechanism to allow your fragment to communicate with your activity, that will then refresh the list. http://stackoverflow.com/questions/10867425/communication-between-fragments-dialogs-in-android – Snicolas Dec 12 '12 at 07:34

2 Answers2

1

Because NotifyDataSetChanged() is just a method to let the adapter to redraw the view but doesn't change the underlying data, you need to let mainListAdapter know the new data by calling either something like mainListAdapter.Clear(), mainListAdapter.Add() to update the data or ArrayAdapter's constructor again.

Aaron He
  • 5,509
  • 3
  • 34
  • 44
0

I scrapped this approach and copied the NotepadV3 example instead.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ron
  • 1,721
  • 1
  • 19
  • 43