RecyclerView项目删除

时间:2015-06-18 23:54:38

标签: java android android-recyclerview

我正在尝试从recyclerview列表中删除特定项目,但我不知道如何处理它。我无法使用notifyItemRemoved方法,因为我收到一条错误,指出无法从静态上下文中使用此方法。正如您所看到的,我在适配器的视图持有者中拥有大部分事件侦听器,因为它更方便。

这是我的代码

public class CartListAdapter extends RecyclerView.Adapter<CartListAdapter.RestaurantHolder> 
{

     List<CartItem> cartList;
    private static String LOG_TAG = "MyRecyclerViewAdapter";
    private static MyClickListener myClickListener;
    static Context context;

    public static class RestaurantHolder extends RecyclerView.ViewHolder
    {
        TextView cartItem;
        TextView cartPrice;
        TextView cartQuantity;
        ImageView increaseIcon;
        ImageView decreaseIcon;
        ImageView deleteIcon;
        int selectedPosition;
        int quantity;
        double price;
        Boolean priceStored = false;

        RestaurantHolder(View itemView)
        {
            super(itemView);
            cartItem = (TextView)itemView.findViewById(R.id.cartItem);
            cartPrice = (TextView)itemView.findViewById(R.id.cartItemPrice);
            cartQuantity = (TextView)itemView.findViewById(R.id.itemQuantity);
            increaseIcon = (ImageView)itemView.findViewById(R.id.increase);
            decreaseIcon = (ImageView)itemView.findViewById(R.id.decrease);
            deleteIcon = (ImageView)itemView.findViewById(R.id.delete);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectedPosition = getPosition();
                    Log.i(LOG_TAG,"Position = "+selectedPosition );
                }
            });

            increaseIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    if(!priceStored)
                        price = Double.parseDouble(cartPrice.getText().toString());

                    priceStored = true;
                    quantity = Integer.parseInt(cartQuantity.getText().toString()) + 1;
                    cartQuantity.setText(String.valueOf(quantity));

                    cartPrice.setText(String.valueOf(price * quantity));

                }
            });
            decreaseIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(quantity > 1) {
                        quantity = Integer.parseInt(cartQuantity.getText().toString()) - 1;
                        cartQuantity.setText(String.valueOf(quantity));
                        cartPrice.setText(String.valueOf(Double.parseDouble(cartPrice.getText().toString())-price));
                    }else{
                        Toast.makeText(context,"Minimum order limit",Toast.LENGTH_SHORT).show();
                    }
                }
            });

            deleteIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    selectedPosition = getPosition();
                    Log.i(LOG_TAG,"Removal Position = "+selectedPosition );

                    //remove(selectedPosition);
                }
            });
        }
    }

    CartListAdapter(Context context)
    {
        this.context = context;
        cartList = new ArrayList<CartItem>();
    }

    public void addItem(CartItem cartItem)
    {

        cartList.add(cartItem);

    }

    public void remove(int position)
    {
        cartList.remove(position);
        notifyItemRemoved(position);
    }



    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView)
    {
        super.onAttachedToRecyclerView(recyclerView);
}

    @Override
    public RestaurantHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_item,viewGroup,false);
        RestaurantHolder restaurantHolder = new RestaurantHolder(view);
        return restaurantHolder;
    }

    @Override
    public void onBindViewHolder(RestaurantHolder restaurantHolder, int i) {

        restaurantHolder.cartQuantity.setText(String.valueOf(cartList.get(i).quantity));
        restaurantHolder.cartPrice.setText(String.valueOf(cartList.get(i).price));
        restaurantHolder.cartItem.setText(cartList.get(i).cartItem);
    }

    @Override
    public int getItemCount() {
        return cartList.size();
    }

    public interface MyClickListener {
        public void onItemClick(int position, View v);
    }
}

1 个答案:

答案 0 :(得分:1)

我从持有者类中删除了静态,现在一切正常