Remove item from gridView, but in Tablet show that deleted last item.How to fix this?

260 views Asked by At

I add item into gridView (use ArrayList with 10 items), but when I click button to remove item (for ex. item index of 2), it remove correct item from ArrayList but in gridView remove last one.

This is Adapter getView() `

    public View getView(int arg0, View convertView, ViewGroup arg2) {

    View row = convertView; OrderItemHolder holder;
    OrderItem mItem = orderItemsList.get(arg0);

    if (row == null) {

        isNew = true;

        LayoutInflater inflater = (LayoutInflater) orderActivity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.order_item, arg2, false);

        holder = new OrderItemHolder();
        // initialize the elements
        holder.itemOrderRLay = (RelativeLayout) row.findViewById(R.id.itemOrderRLay);

        holder.orderItemImage = (CImageView) row.findViewById(R.id.orderItemImage);

        holder.itemCount = (TextView) row.findViewById(R.id.itemCount);
        holder.itemCount.setId(mItem.getCountId());

        holder.itemText = (TextView) row.findViewById(R.id.itemText);
        holder.itemCost = (TextView) row.findViewById(R.id.itemCost);

        holder.maxCount = (CButton) row.findViewById(R.id.maxCount);
        holder.maxCount.setCountViewId(holder.itemCount.getId());

        holder.minCount = (CButton) row.findViewById(R.id.minCount);
        holder.minCount.setCountViewId(holder.itemCount.getId());

        row.setTag(holder);
    } else {
        holder = (OrderItemHolder) row.getTag();
    }

    if (mItem != null) {
        if(isNew){

            isNew = false;

            holder.itemCount.setText("x" + mItem.getCount());
            //holder.itemCount.setId(mItem.getCountId());

            holder.itemText.setText(mItem.getMenuInsideItem().getName());
            holder.itemText.setBackgroundResource(R.drawable.small_title_bg);

            holder.itemCost.setText("(" + mItem.getMenuInsideItem().getTime() + " dəq | " + mItem.getMenuInsideItem().getCost() + " manat)");

            holder.itemCost.setTextColor(Color.rgb(93, 40, 40));

            Drawable d = mContext.getResources().getDrawable(mItem.getMenuInsideItem().getPicId());
            holder.orderItemImage.setImageBitmap(OrderUtil.roundCornerImage(OrderUtil.drawableToBitmap(d), 9));

            holder.maxCount.setIndex(arg0);         
            holder.maxCount.setOnClickListener(this);

            holder.minCount.setIndex(arg0);
            holder.minCount.setOnClickListener(this);


            holderList.add(holder);
        }
    }

    return row;
}`

gridView item xml code

`

                        <emenu.custom.CImageView
                            android:id="@+id/orderItemImage"
                            android:layout_width="165dp"
                            android:layout_height="165dp"
                            android:layout_centerVertical="true"
                            android:background="@drawable/image_bg_thin"
                            android:clickable="false"
                            android:minWidth="50dip"
                            android:scaleType="fitXY"
                            android:src="@drawable/a10" />

                        <TextView
                            android:id="@+id/itemCost"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignBottom="@+id/orderItemImage"
                            android:layout_marginLeft="2dp"
                            android:layout_toRightOf="@+id/orderItemImage"
                            android:gravity="center"
                            android:text="deq|mnt"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="@android:color/black"
                            android:textSize="12sp" />

                        <emenu.custom.CButton
                            android:id="@+id/minCount"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="2dp"
                            android:layout_toRightOf="@+id/maxCount"
                            android:background="@drawable/image_bg_full"
                            android:text="-"
                            android:textColor="@android:color/white" />

                        <emenu.custom.CButton
                            android:id="@+id/maxCount"
                            android:layout_width="50dp"
                            android:layout_height="50dp"
                            android:layout_centerVertical="true"
                            android:layout_marginLeft="2dp"
                            android:layout_toRightOf="@+id/orderItemImage"
                            android:background="@drawable/image_bg_full"
                            android:text="+"
                            android:textColor="@android:color/white" />

                        <TextView
                            android:id="@+id/itemText"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_above="@+id/itemCount"
                            android:layout_alignBottom="@+id/orderItemImage"
                            android:layout_alignLeft="@+id/orderItemImage"
                            android:layout_alignRight="@+id/orderItemImage"
                            android:layout_marginBottom="4dp"
                            android:layout_marginLeft="5dp"
                            android:layout_marginRight="5dp"
                            android:gravity="center"
                            android:text="Pasta Florentine Casserole"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="@android:color/black"
                            android:textSize="13sp" />

                        <TextView
                            android:id="@+id/itemCount"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_above="@+id/itemCost"
                            android:layout_alignLeft="@+id/itemCost"
                            android:layout_marginLeft="2dp"
                            android:gravity="center"
                            android:text="x1"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="@android:color/black"
                            android:textSize="24sp" />

</RelativeLayout>`

And button click function `

        case R.id.minCount: countText = (TextView) holder.itemOrderRLay.findViewById(holder.minCount.getCountViewId());

        count = OrderUtil.updateDBCount((int) mItem.getMenuInsideItem().getId(), deviceId, mItem.getCountId(), mContext, false);

        Log.w(TAG, "(line 179) button.getIndex() = " + button.getIndex());
        Log.w(TAG, "(line 180) holder.minCount.getCountViewId() = " + holder.minCount.getCountViewId());
        Log.w(TAG, "(line 181) count = " + count);
        Log.w(TAG, "(line 182) holder.getIndex() = " + holderList.indexOf(holder));

        if(count != 0){         
            countText.setText("x" + count);                             
        }

        if(count == 0){
            OrderUtil.updateDBCount((int) mItem.getMenuInsideItem().getId(), deviceId, mItem.getCountId(), mContext, false);
            Log.w(TAG, "(line 192) button.getIndex() = " + button.getIndex());


            orderItemsList.remove(button.getIndex());

            this.notifyDataSetChanged();


        }

        totalCost -= mItem.getMenuInsideItem().getCost();
        totalCostText.setText(totalCost + " manat");
        break;

    default:
        break;
    }`
1

There are 1 answers

12
Tzoiker On

It seems that after notifyDataSetChanged() views, that are used in gridview, are not recycled, so in getView() no new views are created and the isNew equals false. This way nothing is changed until theese views will be destroyed. The last item is not shown , because getView() got called so many times as the number of items, which was reduced. So, you should remove isNew check.