How do I add an item to a listView when a click event occurs?

0

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

    
asked by anonymous 17.11.2017 / 17:02

1 answer

1
  • Save a reference from the List that you pass as a parameter to the adapter. Also keep reference to the adapter

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

    public void onButtonClick(View v) {
        Torneio torneio = new Torneio("Torneio " + i);
        this.torneios.add(torneio);
        this.adapter.notifyDataSetChanged();
    }
    
  • 18.11.2017 / 15:50