Miko M Miko M -4 years ago 68
Android Question

How do I update a ListView?

Hi guys I have a problem. I have a DialogFragment with content that is saved in a sqlite db on the phone, and a Fragment that retrieves the information and shows it. The ListView initially (upon creation) shows everything like its supposed to, but when the stuff is updated inside the Dialog Fragment, I dont know how to update the adapter for ListView. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;

namespace Zrelya.Fragments
{
public class OnSelectedEventArgs : EventArgs
{
public ORM.Plan Plan { get; set; }

public OnSelectedEventArgs( ORM.Plan plan )
{
Plan = plan;
}
}

public class ViewPlans : FragmentSuper
{
private Context mContext;
private ORM.DBRep dbr;

public EventHandler<OnSelectedEventArgs> OnSelected;

public ViewPlans(Context context)
{
mContext = context;
}

public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);

// Create your fragment here

dbr = new ORM.DBRep();
}

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Use this to return your custom view for this Fragment
var view = inflater.Inflate(Resource.Layout.ViewPlans, container, false);
var listView = view.FindViewById<ListView>(Resource.Id.listView);


List<ORM.Plan> plansList = dbr.GetPlans();
Adapters.Plan adapter = new Adapters.Plan(mContext, plansList);
listView.Adapter = adapter;

listView.ItemClick += (o, e) =>
{
int id = plansList[e.Position].Id;

OnSelected.Invoke(this, new OnSelectedEventArgs(plansList[e.Position]));
};

return view;
}
}
}


OnSelected.Invoke is what happens when an item is clicked, showing the dialog fragment. The following code is a snippet from MainActivity OnCreate method:

fragmentViewPlans.OnSelected += (o, e) =>
{
int id = e.Plan.Id;
DialogViewPlan(e.Plan);
};


...and the DialogViewPlan method is below:

private void DialogViewPlan(ORM.Plan plan)
{
if (plan != null)
{
Android.App.FragmentTransaction transaction = FragmentManager.BeginTransaction();
Helpers.DialogViewPlan dialog = new Helpers.DialogViewPlan(this, plan);
dialog.Show(transaction, "dialog");
dialog.OnDelete += delegate
{
Toast.MakeText(this, "Plan deleted...", ToastLength.Short).Show();
};
dialog.OnSave += delegate
{
Toast.MakeText(this, "Plan saved!", ToastLength.Short).Show();
};

}
}


I dont know how to talk between activity, fragment and dialog fragment, does anyone know how to do this?

Answer Source

I found an answer:

using Android.App;
using Android.Content;
using Android.OS;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;

namespace Zrelya.Fragments
{
    public class OnSelectedEventArgs : EventArgs
    {
        public ORM.Plan Plan { get; set; }

        public OnSelectedEventArgs( ORM.Plan plan )
        {
            Plan = plan;
        }
    }

    public class ViewPlans : FragmentSuper
    {
        private Context mContext;
        private ORM.DBRep dbr;

        private static Adapters.Plan Adapter;
        private static ListView listView;

        public EventHandler<OnSelectedEventArgs> OnSelected;

        public ViewPlans(Context context)
        {
            mContext = context;
        }

        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here

            dbr = new ORM.DBRep();
        }

        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            // Use this to return your custom view for this Fragment
            var view = inflater.Inflate(Resource.Layout.ViewPlans, container, false);
            listView = view.FindViewById<ListView>(Resource.Id.listView);


            List<ORM.Plan> plansList = dbr.GetPlans();
            Adapter = new Adapters.Plan(mContext, plansList);


            listView.Adapter = Adapter;


            listView.ItemClick += (o, e) =>
            {
                int id = plansList[e.Position].Id;

                var plan = plansList[e.Position];

                DialogViewPlan(plan);
            };

            return view;
        }

        private void DialogViewPlan(ORM.Plan plan)
        {
            if (plan != null)
            {
                FragmentTransaction transaction = Activity.FragmentManager.BeginTransaction();
                Helpers.DialogViewPlan dialog = new Helpers.DialogViewPlan(Activity, plan);
                dialog.Show(transaction, "dialog");
                dialog.OnDelete += delegate
                {
                    Adapter = new Adapters.Plan(mContext, dbr.GetPlans());
                    listView.Adapter = Adapter;
                };
                dialog.OnSave += delegate
                {
                    Adapter = new Adapters.Plan(mContext, dbr.GetPlans());
                    listView.Adapter = Adapter;
                };

            }
        }
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download