Display certain arraylist, with the click of a button

0

Hello, My xml has several buttons how do I when I click on a particular button load an arraylist in the listview

example

Botão1 clicado -> arraylist<Perguntas> carrega na listview1
Botão2 clicado -> arraylist<Respostas> carrega na listview1
Botão3 clicado -> arraylist<Nulas> carrega na listview1

asked by anonymous 10.02.2018 / 20:18

1 answer

0

Just change the Adapter of your ListView. For example:

botao1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             mudaLista(listaDePerguntas);

    });
botao2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
             mudaLista(listaDeRespostas);

    });

Now in your Activity, create the method that changes the list

private void mudaLista(List<Texto> lista){
    listView.setAdapter(new ArrayAdapter<Texto>(this,  R.layout.support_simple_spinner_dropdown_item, lista));
}

It's important to remember 2 things:

1) The Questions, Answers and Nulls classes must be daughters of the Text class. If you already have a parent in your code, use it instead of Text.

2) Your ListView has to be a field of your class, ie it has to be declared out of methods, and initialized in onCreate

    
11.02.2018 / 21:20