I would like to display a Android Spinner which shows data from a SQLite DB.
The user however should be able to add new items to this Spinner.
On the iPhone / iOS I use ActionSheetPicker to achive this goal. It looks like this:
How to to this with Android?
Not sure if it works with a CursorAdapter but
the following code does about the same as the UIPicker on iOS:
The code does the following:
The add-button creates another popup with a EditText (text-field)
ArrayList<String> items = new ArrayList<String>();
items.add("One");
items.add("Two");
items.add("Three");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, items);
final Button selectBtn = (Button) findViewById(R.id.buttonCountry);
selectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(@SuppressWarnings("unused") View v) {
new AlertDialog.Builder(main)
.setTitle("Please choose a country")
.setPositiveButton("Add Country", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
final EditText view = new EditText(main);
new AlertDialog.Builder(main)
.setTitle("Please enter a name")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
adapter.add(view.getText().toString());
selectBtn.setText(view.getText().toString());
}
})
.setNegativeButton("cancel", null)
.setView(view)
.show();
}
})
.setAdapter(adapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
selectBtn.setText(adapter.getItem(which));
dialog.dismiss();
}
}).create().show();
}
});
Here are the resulting pictures:
Activity:
AlertDialog with Spinner and Button:
AlertDialog with text input: