0

I have a ListView that when I click on an item inside it, a dialog fragment appears with a Save and Delete button. Everything works, and it updates the adapter correctly, removing or updating the items from the ListView. Now the problem is that when I delete the top item, or the item above another item, but lets just say there are two items because its easier. When I delete the top item, it's removed from the ListView, but when I click on the same spot, because now the bottom item moves up to the top, the old item data appears in the DialogFragment. In other words, I'm selecting the old item. Here is the code:

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

            }
        }
    }
} 
Miko M
  • 29
  • 7
  • On this line `Adapter = new Adapters.Plan(mContext, dbr.GetPlans());` are you sure `GetPlans()` returns all plans except the one selected? For both OnSave and OnDelete, you could get the values `var plans = dbr.GetPlans();` and put a breakpoint after that to see whats inside. I'm thinking your selected Plan never leaves `plansList`. – Keyur PATEL Jun 13 '17 at 02:29
  • Above `var plan = plansList[e.Position];` I put a line with code: `var plansList = dbr.GetPlans()` to update the list with new plans but it didnt work.. – Miko M Jun 13 '17 at 02:31
  • ur right, plansList never gets updated. how can i do that? – Miko M Jun 13 '17 at 02:36
  • Maybe you could keep `plansList` as a global variable, and inside `OnDelete` you could remove the selected plan from it, and reupdate the Adapter, without `dbr.GetPlans()`. That's because I assume you don't want to actually remove the Plan from your database? – Keyur PATEL Jun 13 '17 at 02:51
  • Yes I do, so Ive made the plansList global, and now call `plansList.Remove(plan);` from inside OnDelete. Also, I've called `Activity.RunOnUiThread(()=> { Adapter.NotifyDataSetChanged(); });`. Its the same result. – Miko M Jun 13 '17 at 02:57
  • If its not too much trouble, could you update the code in the question with your modifications so I can help you double check? – Keyur PATEL Jun 13 '17 at 03:27
  • ive updated the code – Miko M Jun 13 '17 at 03:38
  • do you have any ideas? – Miko M Jun 13 '17 at 07:57
  • This line here `plansList = dbr.GetPlans();` before `DialogViewPlan(plansList[e.Position]);`. Doesn't that reset the plansList? Try removing that line. – Keyur PATEL Jun 13 '17 at 08:10
  • Also apparently, you don't need to re-declare Adapter (`Adapter = new Adapters.Plan(mContext, plansList);`), the `NotifyDataSetChanged()` handles updating, I'll try to post an answer with your code, but trimmed. – Keyur PATEL Jun 13 '17 at 08:18

1 Answers1

0

With some help from a few links:

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

Remove ListView item by clicking button on the item

The following is your trimmed code, try it.

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

            }
        }
    }
} 
Keyur PATEL
  • 2,299
  • 1
  • 15
  • 41