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) ...
).
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
– Boneco Sinforoso
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 ?
– Alex Ferreira
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.
– Boneco Sinforoso
I think I understand now.
– Alex Ferreira