The last Listview item is always deleted even by deleting an item in the middle of Arraylist

Asked

Viewed 98 times

0

I own a Adapter any and by clicking on an delete icon, the item in my ArrayList is removed. The problem is that even by removing the desired item, the ListView is updated, always removing the last element. The curious thing is that if I add a new item to the ArrayList, the list is updated with the actual values.

Here is 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 3 {1,2,4} - Only, Listview displays {1,2,3}, even my Arraylist not containing 3, and containing 4.

Thank you! (:

1 answer

1


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

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

  • Thank you so much! It was really what I wanted!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.