Search code examples
androidiosbuttonuiactionsheetpicker

Place an Add button to an android picker with SimpleCursorAdapter


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: iPhone ActionSheetPicker with Add button

How to to this with Android?


Solution

  • 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:

    1. Create / get the data for the Spinner
    2. Use Buttons instead of Spinners on the activity
    3. Create an AlertDialog (pop-up) which displays the Spinner and an add-button
    4. 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:
    Activity

    AlertDialog with Spinner and Button:
    AlertDialog with Spinner and Button

    AlertDialog with text input:
    AlertDialog with text input