Explanation: I know this question asked no. of times. Try to understand my problme first.I have a listview in which i used a BaseAdapter to show dynamic item on my listivew row and it is not a fixed. I delete the data included into the row but deleted the row. While i delete a row it's data removed completely but not a listview row. I want to remove a particular row from the list.
Adapter
public class CartAdapter extends BaseAdapter {
private static final String TAG=CartAdapter.class.getSimpleName();
public Context context;
public List<CartItems> cartItemList;
public LayoutInflater inflater;
public AppCompatActivity activity;
public CartAdapter(Context context, List<CartItems> cartItemList,AppCompatActivity activity) {
this.context = context;
this.cartItemList = cartItemList;
this.activity=activity;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return this.cartItemList.size();
}
@Override
public Object getItem(int position) {
return this.cartItemList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class CartHolder {
TextView txtItemName, txtItemPrice, txtQtyCount;
ImageButton btnImgDelete;
}
@Override
public View getView(final int position, View view, ViewGroup viewGroup) {
final CartHolder holder;
if (view == null) {
holder = new CartHolder();
view = inflater.inflate(R.layout.cart_items, null);
holder.txtItemName = (TextView) view.findViewById(R.id.txt_item_name);
holder.txtItemPrice = (TextView) view.findViewById(R.id.txt_item_price);
holder.txtQtyCount = (TextView) view.findViewById(R.id.txt_qty_count);
holder.btnImgDelete=(ImageButton)view.findViewById(R.id.btn_img_delete);
holder.btnImgDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View view) {
final AlertDialog.Builder alertDialog=new AlertDialog.Builder(activity,R.style.MyAlertDialogStyle);
alertDialog.setTitle("Delete");
alertDialog.setIcon(R.drawable.ic_action_cancel);
alertDialog.setMessage("Do you really want to delete item??");
alertDialog.setCancelable(true);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
cartItemList.remove(position);
notifyDataSetChanged();
}
});
alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.show();
}
});
view.setTag(holder);
} else {
holder = (CartHolder) view.getTag();
}
CartItems cartItems = cartItemList.get(position);
holder.txtItemName.setText(cartItems.getItemName());
holder.txtItemPrice.setText(Util.priceTODollar(cartItems.getItemPrice()));
holder.txtQtyCount.setText("" + cartItems.getItemQty());
return view;
}
}
Screenshot before delete an item from the listview
Screenshot after delete an item from the listview
In the second screenshot second item deleted but the white background color remain same.
Please, help me to solve out this problem.