How to add an item to a listview when a button-click event occurs?

Asked

Viewed 174 times

0

Guys, I want to create a list of tournaments. Where each item will be added at the click of a create tournament button, following the pattern: tournament 1, tournament 2, tournament 3 ...

1 answer

1

  1. Keep a List reference that you will pass as a parameter to Adapter. Also save the reference to Adapter

    private final List<Torneio> torneios = new ArrayList<>();
    private MyAdapter adapter;
    
  2. In the button click event, add an element to the list and then ask Adapter to update the list

    public void onButtonClick(View v) {
        Torneio torneio = new Torneio("Torneio " + i);
        this.torneios.add(torneio);
        this.adapter.notifyDataSetChanged();
    }
    

Browser other questions tagged

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