这个ListView里面发生了什么?

时间:2017-06-13 02:21:06

标签: c# android listview

我有一个ListView,当我点击其中的一个项目时,会出现一个带有“保存并删除”按钮的对话框片段。一切正常,它正确更新适配器,从ListView中删除或更新项目。现在的问题是,当我删除顶部项目,或上面另一项目的项目,但我们只是说有两个项目,因为它更容易。当我删除顶部项目时,它已从ListView中删除,但当我点击同一位置时,因为现在底部项目向上移动到顶部,旧项目数据将出现在DialogFragment中。换句话说,我选择旧项目。这是代码:

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 ViewPlans : FragmentSuper
    {
        private Context mContext;
        private ORM.DBRep dbr;

        private static Adapters.Plan Adapter;
        private static ListView listView;
        private static List<ORM.Plan> plansList = new List<ORM.Plan>();


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

            plansList = dbr.GetPlans();
            Adapter = new Adapters.Plan(mContext, plansList);
            listView.Adapter = Adapter;

            listView.ItemClick += (o, e) =>
            {
                Activity.RunOnUiThread(()=> {
                    plansList = dbr.GetPlans();
                    DialogViewPlan(plansList[e.Position]);
                });

            };

            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
                {


                    Activity.RunOnUiThread(()=> {
                        plansList.Remove(plan);
                        Adapter = new Adapters.Plan(mContext, plansList);
                        Adapter.NotifyDataSetChanged();
                        listView.Adapter = Adapter;
                    });



                };
                dialog.OnSave += delegate
                {
                    Adapter = new Adapters.Plan(mContext, dbr.GetPlans());

                    Adapter.NotifyDataSetChanged();
                    listView.Adapter = Adapter;
                };

            }
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

在一些链接的帮助下:

Android: how to remove an item from a listView and arrayAdapter

Remove ListView item by clicking button on the item

以下是您的修剪代码,请尝试一下。

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 ViewPlans : FragmentSuper
    {
        private Context mContext;
        private ORM.DBRep dbr;

        private static Adapters.Plan Adapter;
        private static ListView listView;
        private static List<ORM.Plan> plansList = new List<ORM.Plan>();


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

            plansList = dbr.GetPlans();
            Adapter = new Adapters.Plan(mContext, plansList);
            listView.Adapter = Adapter;

            listView.ItemClick += (o, e) =>
            {
                Activity.RunOnUiThread(()=> {
                    //plansList = dbr.GetPlans();
                    DialogViewPlan(plansList[e.Position]);
                });

            };

            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
                {


                    Activity.RunOnUiThread(()=> {
                        plansList.Remove(plan);
                        //Adapter = new Adapters.Plan(mContext, plansList);
                        Adapter.NotifyDataSetChanged();
                        //listView.Adapter = Adapter;
                    });



                };
                dialog.OnSave += delegate
                {
                    //Adapter = new Adapters.Plan(mContext, dbr.GetPlans());

                    Adapter.NotifyDataSetChanged();
                    //listView.Adapter = Adapter;
                };

            }
        }
    }
}