user3418877
user3418877

Reputation: 179

Recycleviewer Adapter NotifyDataSetChanged not work?

I'm using PagerSlidingTabStrip for create tabs and in each tab i have Recyclerview to poupulate related tab items . for example we have 3 tabs with names of Toyota , Benz and Ford and A tab tho show All of items, so in Toyota tab we have vehicles of it and so on. Recyclerview items have a button for add to cart and remove from cart and a Textview to show the number of order .Remove button and Textview is hidden by default and when user add an item to cart so remove button and Textview visiblility will true . The problems is that if a user adds an item to the cart then Remove button and Textview of related item is not Visible in All tab?! But in All tab when user add an item to cart it reflected to item in related tab. I think the problem is the Adapter of Recylerview , it holds the view and not refresh the tab of All items , maybe .I use NotifyDataSetChanged of Recyclerview Adapter but not work.

The button to add to cart and remove: mDecremtBtn and mIncrementBtn when clicked visible Textview and decrement button.

mDcrementBtn.Click += delegate
                    {

                        if (orderList.Count == 0)
                        {
                            quntitySum = 0;
                        }

                        //Find order and decrement
                        var orderItem = orderList.Find(p => p.FoodId == foodId);
                        orderItem.Quantity--;
                        _quantitiyTxt.Text = orderItem.Quantity.ToString();

                        if (orderItem.Quantity == 0)
                        {
                            mDcrementBtn.Visibility = ViewStates.Invisible;
                            _quantitiyTxt.Visibility = ViewStates.Invisible;
                            _quantitiyTxt.Text = "";
                            orderList.Remove(orderItem);
                        }
                        if (orderList.Count == 0)
                        {
                            _fabCart.Visibility = ViewStates.Gone;

                            //Update bageNotification On bottomNavigation Cart when no items in orderlist
                            _badgeNotification.Text = "";
                            _badgeNotification.Visibility = ViewStates.Gone;
                        }
                        _fabCart.SetImageBitmap(ConvertTextToBitmap.TextAsBitmap((--quntitySum).ToString(), 36,
                           Color.White));

                        //Update bageNotification On bottomNavigation Cart
                        _badgeNotification.Text = quntitySum.ToString();
                        _adpter.NotifyDataSetChanged();

                    };

                    mIncrementBtn.Click += delegate
                    {
                        if (orderList.Count == 0)
                        {
                            quntitySum = 0;
                        }
                        _fabCart.Show();
                        mDcrementBtn.Visibility = ViewStates.Visible;
                        _quantitiyTxt.Visibility = ViewStates.Visible;
                        if (orderList.Any(p => p.FoodId == foodId))
                        {
                            //Find order and increment
                            var orderItem = orderList.Find(p => p.FoodId == foodId);

                            orderItem.Quantity++;
                            _quantitiyTxt.Text = orderItem.Quantity.ToString();
                        }
                        else
                        {
                            orDetail = new Order
                            {
                                FoodId = foodId,
                                FoodName = mFoodNameTxt.Text,
                                Price = Convert.ToInt32(mPriceTxt.Text),
                                ClickFlag = true
                            };

                            orDetail.Quantity++;
                            orderList.Add(orDetail);
                            _quantitiyTxt.Text = orDetail.Quantity.ToString();
                        }

                        _fabCart.SetImageBitmap(ConvertTextToBitmap.TextAsBitmap((++quntitySum).ToString(), 36,
                            Color.White));

                        //Update bageNotification On bottomNavigation Cart
                        _badgeNotification.Visibility = ViewStates.Visible;
                        _badgeNotification.Text = quntitySum.ToString();

                        _adpter.NotifyDataSetChanged();

                    };

In OnbindeViewHolder of class SimpleStringRecyclerViewAdapter : RecyclerView.Adapter items are update.

public override  void OnBindViewHolder(RecyclerView.ViewHolder holder, int position)
                {
                    var simpleHolder = holder as SimpleViewHolder;    

                    simpleHolder.mFoodNameTxt.Text = _foodItems[position].FoodName;
                    simpleHolder.mPriceTxt.Text = _foodItems[position].Price.ToString();
                    simpleHolder.foodId = _foodItems[position].Id;   

                    var item = orderList.Find(p => p.FoodId == _foodItems[position].Id);
                    if (item != null && item.ClickFlag)
                    {
                        simpleHolder.mDcrementBtn.Visibility = ViewStates.Visible;
                        simpleHolder._quantitiyTxt.Visibility = ViewStates.Visible;
                        simpleHolder._quantitiyTxt.Text = item.Quantity.ToString();

                    }
                    else
                    {
                        simpleHolder.mDcrementBtn.Visibility = ViewStates.Invisible;
                        simpleHolder._quantitiyTxt.Visibility = ViewStates.Invisible;
                        simpleHolder._quantitiyTxt.Text = "";
                    }

                }

The senario of it in Imagenary : enter image description here

Compelete code in https://forums.xamarin.com/discussion/120389/recycleviewer-adapter-notifydatasetchanged-not-work/p1?new=1

Upvotes: 0

Views: 354

Answers (0)

Related Questions