The last item in the ListView is always deleted, even deleting an item in the middle of the ArrayList

0

I have Adapter any, and when I click on an exclude icon, the item of my ArrayList is removed. The problem is that even removing the desired item, ListView is updated, always removing the last element. The curious thing is that if I add a new item to ArrayList , the list is updated with the actual values.

Here's my code:

    @Override
public View getView(int position, View convertView, ViewGroup parent) {

    //final ViewHolder holder;
    View v = convertView;
    final int pos = position;

    if (v == null) {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        v = vi.inflate(R.layout.adapter_product_list, null);

        //holder = new ViewHolder();

        if (productList.size() > 0) {
            //Product name
            final Product product = productList.get(position);
            final TextView productName = (TextView) v.findViewById(R.id.productName);
            productName.setEnabled(false);

            //Edit Button
            ImageButton editButton = (ImageButton) v.findViewById(R.id.editButton);

            //Delete button
            final ImageButton  deleteButton = (ImageButton) v.findViewById(R.id.deleteButton);
            deleteButton.setTag(position);

            try {
                if (productName != null)
                    productName.setText(String.valueOf(product.getDescription()));

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

            //Listeners
            editButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    productName.setEnabled(true);
                    productName.requestFocus();
                }
            });

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

                    AlertDialog.Builder builder = new AlertDialog.Builder(view.getRootView().getContext());

                    Integer tag = (Integer) view.getTag();
                    newDialogBuilder(tag);

                    notifyDataSetChanged();

                    String yes = context.getResources().getString(R.string.yes);
                    String no = context.getResources().getString(R.string.no);

                    builder.setMessage(context.getResources().getString(R.string.areYouSureDelete))
                            .setPositiveButton(yes, dialogDelete)
                            .setNegativeButton(no, dialogDelete).show();

                }
            });

        }
    }
    return v;
}

private void newDialogBuilder(final int position) {
    this.dialogDelete = new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            switch (which){
                case DialogInterface.BUTTON_POSITIVE:

                    productList.remove(position);
                    ProductListAdapter.this.notifyDataSetChanged();
                    break;

                case DialogInterface.BUTTON_NEGATIVE:
                    break;
            }

            ProductListAdapter.this.notifyDataSetChanged();
        }
    };
}

Example: - My list has {1,2,3,4} - Remove the 3 {1,2,4} - Only, the ListView displays {1,2,3}, even my ArrayList does not contain 3, and contains the 4.

Thank you! (:

    
asked by anonymous 24.01.2016 / 17:22

1 answer

1

Delete the if (v == null){ line and its } line.

As this block is only executed when convertView is null, when it is not, then a view used by another item will be reused, causing the value stored in view.tag does not match the position of the clicked item.

    
24.01.2016 / 18:16