使用notifyDataSetChanged()不刷新GridView

时间:2018-01-18 04:38:46

标签: android

我使用GridView在两列中显示数据列表。现在,当我对列表的数据进行一些更改并调用notifyDataSetChanged()时,它的第一手资料并不令人耳目一新。当我滚动视图然后它反映了变化。如何找到解决方法?请帮忙

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

`

这就是我设置适配器的方式: -

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

我从一个函数调用notifyDataSetChanged(),该函数在click事件(例如将bean.getQuantity()值设置为1)之后调用,如下所示:

menuItemAdapter.notifyDataSetChanged();

2 个答案:

答案 0 :(得分:1)

第一件事notifyDataSetChanged()只能使用相同的数据集参考。并且您已在适配器的构造函数中创建了一个新列表。所以你无处可去。它应该如此简单。

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

以下是您犯的一些错误 的 1 即可。您已创建新的数据集实例,因此notifyDataSetChanged将无效 的 2 即可。你在构造函数中调用notifyDataSetChanged()为什么?它毫无用处。

所以整点是数据集引用应该是相同的。然后,只有notifyDataSetChanged才会在同一列表中进行更改时才会起作用。

建议: - 使用RecyclerView移至GridLayouManager。它为ViewHolder模式提供了更好的方法。

答案 1 :(得分:0)

在适配器中添加更改ArrayList数据的update()方法。这是一个例子

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

在MainActivity类中,使用update方法更改数据

adapter.update(updatedList);

这样,每次更新列表时都不必创建适配器。