Concatenation of an arrayList into a listview?

Asked

Viewed 536 times

0

I wonder if there’s a way to add "strings" to be exhibited in a listview so that I don’t have to be imposing conditions and more conditions and set again the "strings" already used:

package genesysgeneration.list;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnEspada, btnArco;
    private ListView lvItens;
    private int cont01, cont02;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cont01=0;
        cont02=0;

        lvItens=(ListView)findViewById(R.id.lvItens);
        btnEspada=(Button)findViewById(R.id.btnEspada);
        btnArco=(Button)findViewById(R.id.btnArco);

        btnEspada.setOnClickListener(this);
        btnArco.setOnClickListener(this);

    }

    public void onClick(View v){

        final ArrayList<String> item = new ArrayList<String>();
        ArrayAdapter<String> itens = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, item);

        switch (v.getId()){

            case R.id.btnEspada:

                if (cont01==0){

                    cont01=1;

                    if (cont02==1){

                        item.add("Espada");
                        item.add("Arco");
                        lvItens.setAdapter(itens);

                    }else {

                        item.add("Espada");
                        lvItens.setAdapter(itens);

                    }

                }else {

                    cont01=0;

                    if (cont02==1){

                        item.add("Arco");
                        lvItens.setAdapter(itens);

                    }else {

                        item.add("");
                        lvItens.setAdapter(itens);

                    }

                }

                break;

            case R.id.btnArco:

                if (cont02==0){

                    cont02=1;

                    if (cont01==1){

                        item.add("Espada");
                        item.add("Arco");
                        lvItens.setAdapter(itens);

                    }else {

                        item.add("Arco");
                        lvItens.setAdapter(itens);

                    }

                }else {

                    cont02=0;

                    if (cont01==1){

                        item.add("Espada");
                        lvItens.setAdapter(itens);

                    }else {

                        item.add("");
                        lvItens.setAdapter(itens);

                    }

                }

                break;

        }

    }

}

Something simple, example: There are already 10 items displayed in my listview. I would like to add more items without having to set all others (item.add(item01), item.add(item02) ...).

2 answers

3


The problem is that Arraylist is being created whenever the button is clicked.

Declare it as a class attribute, install it in onCreate() and build the Adapter there as well.

In each case of the method onClick() call the method add() with its value.

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Button btnEspada, btnArco;
    private ListView lvItens;
    private ArrayList<String> itens;
    private ArrayAdapter<String> itensAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvItens=(ListView)findViewById(R.id.lvItens);
        btnEspada=(Button)findViewById(R.id.btnEspada);
        btnArco=(Button)findViewById(R.id.btnArco);

        btnEspada.setOnClickListener(this);
        btnArco.setOnClickListener(this);

        itens = new ArrayList<String>();
        itensAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, itens);
        lvItens.setAdapter(itensAdapter);    
    }

    public void onClick(View v){

        switch (v.getId()){

            case R.id.btnEspada:

                itens.add("Espada");
                itensAdapter.notifyDataSetChanged();
                break;

            case R.id.btnArco:

                itens.add("Arco");
                itensAdapter.notifyDataSetChanged();
                break;

        }

    }

}

1

The insertion logic in listview is as follows:

  1. create an Adapter and set in listview listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, new ArrayList<>())) onCreate()
  2. When retrieving the Listener from a button, use this code to add some item in the listview:

    lvItens.getAdapter().add("Teste");
    lvItens.getAdapter().notifyDataSetChanged(); // Notifica a listView que os dados mudaram e necessita atualizar o view com eles
    

Repeat this process for each listener and you will have a listview with items that the buttons have added. However, this way you can repeat data in the list, if you do not want this, check before, if there is the element in the list, looking inside a for or loop.

  • No, like... I wish I didn’t have to use those conditions that I implemented "if". A way where when clicking the button, it added the item to the end of the listview regardless of what was already in the listview

  • I did not mean the logic of these conditions, why them ? You would like to click a button and add a string to the array, if you already have the string, do nothing, and each button would have a String to add ?

  • No. Forget this "if’s", I would like to know only how to add strings to listview. When I do not set these conditions, and click on the button, the listview appears only the last button clicked. I would like to know how to do (for example) so that if Activity has 10 Buttons, and I click on the 10, 10 items appear in the listview.

  • I think I understand now.

Browser other questions tagged

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