如何更新ListView?

时间:2017-06-09 16:13:00

标签: c# android listview android-fragments

大家好我有问题。我有一个DialogFragment包含保存在手机上的sqlite数据库中的内容,以及一个检索信息并显示它的Fragment。 ListView最初(在创建时)显示了它应该的所有内容,但是当在Dialog Fragment中更新内容时,我不知道如何为ListView更新适配器。这是我的代码:

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

OnSelected.Invoke是单击项目时显示的对话框片段。以下代码是MainActivity OnCreate方法的代码段:

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();
        };

    }
}

...而DialogViewPlan方法如下:

{{1}}

我不知道如何在活动,片段和对话片段之间进行交谈,有谁知道如何做到这一点?

1 个答案:

答案 0 :(得分:0)

我找到了答案:

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;
                };

            }
        }
    }
}