0

I have tried most answers from this site without getting the desired result.

I have recyclerview with a list of items and a button contained in each row. When the button is clicked, I want the entire row to be deleted and list updated automatically.

Here's my adapter class code:

public class CartAdapter extends RecyclerView.Adapter<CartViewHolder> {

private static final String TAG = CartAdapter.class.getSimpleName();

private final Context context;
private List<Cart> itemsList;
private String id;

public CartAdapter(Context context, List<Cart> itemsList) {
    this.context = context;
    this.itemsList = itemsList;
}

@Override
public CartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_items_layout, parent, false);
    return new CartViewHolder(view);
}

@Override
public void onBindViewHolder(final CartViewHolder viewholder, final int position) {
    final Cart cart = itemsList.get(position);
    id = cart.getProductId();

    AndroidNetworking.get(Constants.PRODUCT_DETAILS_ENDPOINT + id)
            .setTag("Get Product Image")
            .setPriority(Priority.HIGH)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Cart Item Image Response:\t" + response.toString());
                    String imagePath = null;
                    try {
                        JSONObject itemObject = new JSONObject(response.toString());
                        JSONObject data = itemObject.getJSONObject("data");
                        imagePath = data.getString("image");

                        Picasso.with(context)
                                .load(imagePath)
                                .placeholder(R.drawable.noimage)
                                .into(viewholder.cartItemImg);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(ANError anError) {

                }
            });

    viewholder.cartItemTitle.setText(cart.getProductTitle());
    viewholder.cartQtyTV.setText(cart.getCartQty() + "");
    viewholder.clearProductIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            removeProductRow(cart);
        }
    });
    viewholder.decreaseCartIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            decreaseProduct();
        }
    });
    viewholder.increaseCartIV.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            increaseProduct();
        }
    });
}

private void increaseProduct() {

}

private void decreaseProduct() {

}

public void removeProductRow(final Cart cart) {
    AndroidNetworking.get(Constants.REMOVE_PRODUCT_CART + id)
            .setTag("Delete Product From Cart")
            .setPriority(Priority.HIGH)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, "Delete Product From Cart Response:\t" + response.toString());

                    int position = itemsList.indexOf(cart);
                    itemsList.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position, itemsList.size());
                }

                @Override
                public void onError(ANError anError) {

                }
            });
}

@Override
public int getItemCount() {
    if (itemsList == null) {
        return 0;
    }
    return itemsList.size();
}
}

This code works and the item is deleted. However, the list does not update until I re-visit the activity again. How can I update the view immediately?

Lucifer
  • 28,605
  • 21
  • 86
  • 137
  • 2
    That's mostly because you perform a Network action to delete the row, which is in a different Thread, try to move code from `onResponse` in a private function of your `CartAdapter` and then call it in `onResponse` – Ionut J. Bejan Mar 28 '18 at 06:36
  • I should remove the code block inside the response? –  Mar 28 '18 at 06:37
  • Can you post your comment as answer, so I can accept it? –  Mar 28 '18 at 06:39
  • Move it into a function let's say `updateData(Cart cart)`, and then just call this function inside `onResponse` – Ionut J. Bejan Mar 28 '18 at 06:39
  • Thanks. That worked –  Mar 28 '18 at 06:41

2 Answers2

4
private update(Cart cart){
      Log.d(TAG, "Delete Product From Cart Response:\t" +
      response.toString());
                int position = itemsList.indexOf(cart);
                itemsList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, itemsList.size());
}

And then, onResponse just call update(cart); <- You may need to pass some other data into this function based on your needs.

Update (related to your second problem):

@Override
public long getItemId(int position) {
    return itemList != null ? itemList.get(position).getId() : 0;
}

Or, you could make a public function that return the full item for you

public Cart getItem(int position){
    return itemList != null itemList.get(position) : null;
} // you have to check if this is null when you call it 
Ionut J. Bejan
  • 675
  • 9
  • 22
1

maybe not UI thread running below code,cause not timely refresh interface @Override public void onResponse(JSONObject response) { Log.d(TAG, "Delete Product From Cart Response:\t" + response.toString());

                int position = itemsList.indexOf(cart);
                itemsList.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, itemsList.size());
            }
苏高军
  • 11
  • 3