Display given arraylist, with click on given button

Asked

Viewed 46 times

0

Hello, My xml has several buttons as I do so that when I click a certain button press an arraylist in 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

always using msm listview, I am currently doing this with various activitys created one for each button I think this is not a good practice.

1 answer

0

Simply change your Listview Adapter. 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 two things:

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

2)Your Listview has to be a field of your class, i.e., it has to be declared out of methods, and initialized in onCreate

Browser other questions tagged

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