There is this adapter

public class ListViewClearAdapter extends BaseAdapter { private Context context; ArrayList<itemSelect> objects; ListViewClearAdapter adapter = this; public ListViewClearAdapter(Context context, ArrayList<itemSelect> itemSelects) { this.context = context; this.objects = itemSelects; } @Override public int getCount() { return objects.size(); } @Override public Object getItem(int position) { return objects.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View customView = convertView; final itemSelect itemSelects = getItemList(position); if (convertView == null) { LayoutInflater li = LayoutInflater.from(context); customView = li.inflate(R.layout.list_item_clear, null); customView.findViewById(R.id.ripple).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { objects.remove(position); adapter.notifyDataSetChanged(); } }); customView.findViewById(R.id.ripple2).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { } }); TextView textView = (TextView) customView.findViewById(R.id.textView); textView.setText(itemSelects.name); } return customView; } itemSelect getItemList(int position) { return ((itemSelect) getItem(position)); } } 

When I click on the button, I delete from the dynamic array of objects and update the adapter.

 customView.findViewById(R.id.ripple).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { objects.remove(position); adapter.notifyDataSetChanged(); } }); 

But instead of removing the desired item, it deletes the items sequentially from the bottom.

    1 answer 1

    You do not reassign handlers when reused.

      customView = covertView; if (customView == null) { LayoutInflater li = LayoutInflater.from(context); customView = li.inflate(R.layout.list_item_clear, null); } customView.findViewById(R.id.ripple).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { objects.remove(position); adapter.notifyDataSetChanged(); } }); 

    Threat Use the ViewHolder pattern (for example , see here ).

    UPD ListView is optimized to reuse view for other items. Those. it creates as much view as it fit on the screen, plus or minus one or two. When scrolling through the list, new views are not created, but previously created ones (the same convertView) are used. In this case, your task in a new position is to replenish the data, rewrite the handlers (depending on the position in particular).

    You have implemented in such a way that the data and handlers are filled only when you first add the view. Scroll down, you will see that the installed text ( textView.setText(itemSelects.name); ) will repeat, moreover, as a hit.
    This is not true. Thus, you need to fill in the data and hang the handlers not only with convertView == null.

    According to the ViewHolder pattern ... It allows you to avoid the extra expensive findViewById. For details, see the link above.

    • I do not understand, it is possible in more detail - Andrew
    • @Fasd updated the answer - Yura Ivanov
    • Thank you, saved the situation) - Andrew