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) ...
).