A
A
Anton @ Lyalin2018-02-13 12:56:27
Android
Anton @ Lyalin, 2018-02-13 12:56:27

How to remove an element from an ArrayAdapter?

How can certain data be removed from an ArrayAdapter? By clicking on items

/*Элементы активити*/
        final EditText name = findViewById(R.id.name);
        final EditText price = findViewById(R.id.price);
        final Button add = findViewById(R.id.add);
        final ListView items = findViewById(R.id.items);

/*отдельный класс с ArrayAdapter<Items>, Items - класс с атрибутами (String, String)*/
        final ItemsAdapter itemsAdapter = new ItemsAdapter(MainActivity.this, R.layout.item);
        items.setAdapter(itemsAdapter);

/*По нажатию забирает данные из edittext и передает в listview*/
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String sName = String.valueOf(name.getText());
                String sPrice = String.valueOf(price.getText());

                if (sName.length() != 0 && sPrice.length() != 0)
                    itemsAdapter.add(new Item(sName, sPrice));
                else
                    Toast.makeText(MainActivity.this, "Warning", Toast.LENGTH_SHORT).show();
            }
        });

items.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(final AdapterView<?> adapterView, View view, final int position, long l) {
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

                builder.setTitle("Remove");
                builder.setMessage("Удалить данную покупку?");

                builder.setPositiveButton("Да", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
/*сюда нужно написать удаление элемента из адаптера*/
                    }
                });

                builder.setNegativeButton("Отмена", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.cancel();
                    }
                });

                builder.show();
            }
        });

/* View of a full-fledged application */
5a82b4f0ba5d8551274079.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2018-02-13
@toxa_1995

Questions like this are one of the reasons why you don't need to use Array/Cursor/Whatever Adapter and ListView in its entirety.
You can take the element by position and then do adapter.remove().
You can save an array in your successor, and manually delete this element from it, completely re-allocating memory.
You can throw out the arrayadapter and write your normal one.
You can throw out the listview along with the adapter and use RecyclerView (it lacks those crazy "simple" adapters, so it will be harder to step on this rake). And it will be good.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question