-4

I am using GridView to show a list of data in two columns. Now when I make some change in data of the list and call notifyDataSetChanged() its not refreshing on first hand. When I scroll the view then it reflects the change. How to find a workaround for this? Please help

public class MenuGridViewAdapter extends BaseAdapter implements 
   View.OnClickListener {

Activity activity;

ArrayList mArraylist = new ArrayList();

private TextView mItemName, mDescription;
private TextView mItemRate;
private TextView mQuantity;
private ImageView mAddItem;
private LinearLayout mInYourCart;
private CardView mContainer;

public MenuGridViewAdapter(Activity activity, ArrayList mArraylist) {
    this.activity = activity;
    this.mArraylist = new ArrayList();
    this.mArraylist.clear();
    this.mArraylist.addAll(mArraylist);
    notifyDataSetChanged();
}

@Override
public int getCount() {
    return mArraylist.size();
}

@Override
public Object getItem(int position) {
    return mArraylist.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = LayoutInflater.from(activity).inflate(R.layout.layout_menu_item, null); // inflate the layout

    mContainer = convertView.findViewById(R.id.container);
    mQuantity = convertView.findViewById(R.id.quantity);
    mDescription = convertView.findViewById(R.id.description);
    mInYourCart = convertView.findViewById(R.id.added_to_cart);
    mItemName = convertView.findViewById(R.id.item_name);
    mAddItem = convertView.findViewById(R.id.add_item);
    mItemRate = convertView.findViewById(R.id.price);

    MenuItemBean bean = (MenuItemBean) mArraylist.get(position);
    // Log.e("getProductName  ", bean.getProductName());
    mItemName.setText("" + bean.getProductName());
    mItemRate.setText("CHF " + bean.getRate());

    if (bean.getDescription() != null) {
        mDescription.setText(bean.getDescription());
    } else {
        mDescription.setText("");
    }

    if (Constants.menuFilter.size() == 0) {
        mContainer.setVisibility(View.VISIBLE);
    } else {

        String filter = "";
        if (bean.getIs_spicy() == 1) {
            filter = "spicy";
        }

        if (bean.getIs_veg() == 1) {
            filter = "Vegetarian";
        }

        int flag = 0;
        for (int i = 0; i < Constants.menuFilter.size(); i++) {
            if (Constants.menuFilter.get(i).equalsIgnoreCase(filter)) {
                flag = 1;
                break;
            }
        }

        if (flag == 1) {
            mContainer.setVisibility(View.VISIBLE);
        } else {
            mContainer.setVisibility(View.GONE);
        }
    }

    if (!bean.getQuantity().equalsIgnoreCase("0")) {
        Log.d("ITEMADAPTER:", "UPDATED ITEMS" + "--" + position + "--VISIBLE");
        mInYourCart.setVisibility(View.VISIBLE);
        mQuantity.setText("" + bean.getQuantity());
    } else {
        Log.d("ITEMADAPTER:", "UPDATED ITEMS" + "--" + position + "--GONE");
        mInYourCart.setVisibility(View.GONE);
        mQuantity.setText("" + bean.getQuantity());
    }

    mAddItem.setTag(Integer.valueOf(position));
    mAddItem.setOnClickListener(this);

    return convertView;
}

@Override
public void onClick(View v) {
    int pos = (int) v.getTag();
    int id = v.getId();

    MenuItemBean bean = (MenuItemBean) mArraylist.get(pos);

    switch (id) {
        case R.id.add_item:
            FragmentMenuCard fragmentMenuCard = new FragmentMenuCard();
            fragmentMenuCard.showAddItemDialog(activity, bean, mQuantity, mInYourCart);
            break;
    }
}

`

And this is how I am setting the Adapter:-

menuItemList = new ArrayList();

        if (getArguments().getSerializable("menuList") != null) {
            menuItemList = (ArrayList) getArguments().getSerializable("menuList");

            menuItemAdapter = new MenuGridViewAdapter(getActivity(), menuItemList);
            mListView.setAdapter(menuItemAdapter);
            Log.d("MenuList:", String.valueOf(menuItemList.size()));
        }

And I am calling notifyDataSetChanged() from a function which is called after an click event ( which set bean.getQuantity() value to 1 for example ) like this:

menuItemAdapter.notifyDataSetChanged();

2 Answers2

1

First thing first notifyDataSetChanged() only will work with the same reference of dataset . And you have created a new list int your adapter's constructor . So you are going nowhere from here . It should as simple as .

private ArrayList mArraylist = new ArrayList();
public MenuGridViewAdapter(Activity activity, ArrayList mArraylist) {
    this.activity = activity;
    this.mArraylist = mArraylist;
}

Below are some mistakes you have made.
1.. You have created a new instance of dataset so notifyDataSetChanged will not work.
2. You have called notifyDataSetChanged() in the constructor why ? Its useless.

So the whole point is dataset reference should be same. Then only notifyDataSetChanged will work only if you made changes in the same list.

Suggestion:- Move to RecyclerView with GridLayouManager. It provide the better approach for ViewHolder pattern.

ADM
  • 20,406
  • 11
  • 52
  • 83
  • Thank you. Will just check it. Made the changes that you mentioned in point 1 and 2 following this link :- [https://stackoverflow.com/questions/15422120/notifydatasetchange-not-working-from-custom-adapter] . Anyways Thank you will apply the changes and check. – Om Prakash Agrahari Jan 18 '18 at 05:41
  • Thanks @ADM . Its working with `RecyclerView` and `GridLayoutManager` but not with `GridView`. Don't know why. – Om Prakash Agrahari Jan 18 '18 at 06:23
  • It will work with `GridView` . give it a try . however you have moved on with `RecyclerView` which is great. Good luck – ADM Jan 18 '18 at 06:27
0

In your Adapter add the update() method which is changes the ArrayList data. Here's an example

public class MenuGridViewAdapter extends BaseAdapter {
   public void update(ArrayList<String> data){
     mArraylist = data;
     notifyDatasetChanged();
   }
}

In your MainActivity class use the update method to change data

adapter.update(updatedList);

That way you don't have to create the adapter everytime the list is updated.

pratham kesarkar
  • 3,770
  • 3
  • 19
  • 29