Reputation: 25
Im tring to add the text thats in an edittext to the spinner when the submit buttin is clicked...
I already have 3 entries in the spinner which are showing up, but the string I make from the edit text is not entering into it.
public void addItemsOnSpinner2() {
FoodSpinner = (Spinner) findViewById(R.id.spinner);
final List<String> list = new ArrayList<String>();
list.add("Bread");
list.add("Sugar");
list.add("Steak");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
FoodSpinner.setAdapter(dataAdapter);
button6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ET = editText.getText().toString();
if (!ET.equals("")) {
list.add("" + ET + "");
}
}
}
Upvotes: 0
Views: 1020
Reputation: 2424
Try this inside the if statement
dataAdapter.add("" + ET + "");
dataAdapter.notifyDataSetChanged();
FoodSpinner.setAdapter(dataAdapter);
Upvotes: 0
Reputation: 16214
It adds the item, simply you don't "refresh" the adapter:
@Override
public void onClick(View v) {
String ET = editText.getText().toString();
if (!ET.equals("")) {
list.add(ET);
dataAdapter.notifyDataSetChanged();
}
}
Upvotes: 1