I'm making an example with Android Studio and I want to add items to a List that is displayed in a Spinner. Looking here, I found this answer that helped me a lot.
Basically I create an ArrayAdapter and assign it to my Spinner with the modified list every time I press a button. I was wondering if there is a more efficient way of doing this.
I have an EditText where I write a string and when I press the button I add it to an ArrayList and use it to fill the Spinner.
This is the code of my MainActivity (btnAgregar_Click is the method executed when you press the button):
public class MainActivity extends AppCompatActivity {
private final List<String> lista = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnAgregar_Click(View v) {
// Create Java objects that refer to XML interface
EditText txtArticulo = findViewById(R.id.txtArticulo);
Spinner spinLista = findViewById(R.id.spinLista);
// Read text in txtArticulo
String articulo = txtArticulo.getText().toString();
// If there is text, add it to the list
if (articulo.length() > 0) {
lista.add(articulo);
txtArticulo.setText("");
} else {
// Otherwise, show an error
Toast.makeText(this,"Error: escribe un artÃculo",Toast.LENGTH_SHORT).show();
}
// Add adapter to the Spinner
ArrayAdapter<String> adapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_item, lista);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinLista.setAdapter(adapter);
}
}
Although it works, I think it's an overkill to create an ArrayAdapter, initialize it and add it to the Spinner every single time I press the button. Is this the best way to do it? Can I just create the adapter once and just change something when I add an item to the ArrayList?