2015-06-18 228 views
1

我想從recyclerview列表中刪除特定的項目,但我不知道如何處理它。我不能使用notifyItemRemoved方法,因爲我得到一個錯誤,說該方法不能從靜態上下文中使用。正如你所看到的,我在適配器的視圖持有者中擁有大部分事件偵聽器,因爲它更方便。RecyclerView項目刪除

這裏是我的代碼

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

我刪除從支架類的靜態,一切現在工作