Remove item from an Adapter class

Asked

Viewed 30 times

0

I’m trying to remove the listview item, but when I test the app does nothing... My class Productoadapter:

public class ProdutoAdapter extends ArrayAdapter<Carrinho_Item> {
    private Context context;
    private List<Carrinho_Item> item;

    public ProdutoAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<Carrinho_Item> objects) {
        super(context, resource, objects);
        this.context = context;
        this.item = objects;
    }

    @Override
    public View getView(final int pos, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.list_carrinho, parent, false);

        TextView txtCodigo = (TextView) rowView.findViewById(R.id.txtCodigoDoProduto);
        TextView txtDescricao = (TextView) rowView.findViewById(R.id.txtProduto);
        TextView txtCor = (TextView) rowView.findViewById(R.id.txtCor);
        TextView txtTamanho = (TextView) rowView.findViewById(R.id.txtTamanho);
        TextView txtQuantidade = (TextView) rowView.findViewById(R.id.txtQuantidade);

        txtCodigo.setText(""+item.get(pos).getCodigo());
        txtDescricao.setText(item.get(pos).getNome());
        txtCor.setText(""+item.get(pos).getCor());
        txtTamanho.setText(item.get(pos).getTamanho());
        txtQuantidade.setText(""+item.get(pos).getQuantidade());

        rowView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.remove(item.get(pos));
            }
        });

        return rowView;
    }
}

1 answer

1


After removing the item (item.remove(...)) you must notify the adapter that there has been a change in the data. This can be done with the method notifyDatasetChanged.

Consider using the standard ViewHolder to optimize the performance of your list. See this article on how to do it.

  • Vlw bro, save ! It worked here

  • Great! Don’t forget to mark the answer as solution.

Browser other questions tagged

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