I would like the text that is in the EditText to be added to the spinner by pressing a button. It should do it again and again, so that more text is always added. The problem with my code is that if you press the button once, the entered text is displayed several times in the spinner and replaces the other texts in there. Thank you for your advise!
Here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter(this, android.R.layout.simple_spinner_dropdown_item,
new ArrayList<CharSequence>());
adapter.addAll("gg");
EditText text = (EditText) findViewById(R.id.editTextTextPersonName);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.add(text.getText());
spinner.setAdapter(adapter);
}
});
}
Source: Android Questions