Concatenating an array List in a listView?

0

I wonder if there is a way to add "strings" to display in a listView so that I do not have to impose conditions and conditions again 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) ... ).

    
asked by anonymous 16.02.2017 / 00:25

2 answers

3

The problem is that the ArrayList is being created every time the button is clicked.

Declare it as an attribute of the class, instantiate it in onCreate (), and build the Adapter there too.

In each case of method onClick() call 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;

        }

    }

}
    
17.02.2017 / 00:07
1

The insertion logic in the listview is as follows:

  • create an adapter and set in the listview listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, new ArrayList<>())) in onCreate ()
  • When retrieving the listener of some 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 added. However, in this way, you can repeat data in the list, if you do not want this, first check if the element exists in the list by looking inside a for or loop.

        
    16.02.2017 / 12:39