Search code examples
androidandroid-alertdialogandroid-xml

Getting the value of selected Spinner item in an alert dialog


I wanted to get the selected spinner item in an AlertDialog. But if I'm trying to print the value with Snackbar I'm getting a NullPointerException. How can I refer to the spinner value in the AlertDialog?

The Exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference.

Dialog's code:

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final AlertDialog alert = builder.create();

    final Spinner spinner = (Spinner)findViewById(R.id.spinner);

    // Set title of the dialog
    builder.setTitle("ToDo hinzufügen");
    // Set view to dialog.xml
    builder.setView(R.layout.dialog);


    // To add ToDos for a specific section
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();*/

            // save entry in database and refresh the page
            builder.setPositiveButton("Hinzufügen", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    alert.dismiss();


                    Snackbar.make(findViewById(R.id.fab), spinner.getSelectedItem().toString(), Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

dialog.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp">

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="20dp"
        android:hint="Kategorie auswählen"
        android:entries="@array/tab_categeory"
        android:spinnerMode="dialog"
        />

    <EditText
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Was möchten Sie erledigen?"/>
</LinearLayout>

Solution

  • Try this bro!

        private String value;
                final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    
                final Spinner spinner = (Spinner)findViewById(R.id.spinner);
    
                // Set title of the dialog
                builder.setTitle("ToDo hinzufügen");
                // Set view to dialog.xml
                builder.setView(R.layout.dialog);
    
                spinner .setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
                        value= adapterView.getItemAtPosition(position).toString;
    
                    }
    
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {
                    }
                });
    
     final AlertDialog alert = builder.create();
    
                // To add ToDos for a specific section
                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        /*Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                                .setAction("Action", null).show();*/
    
                        // save entry in database and refresh the page
                        builder.setPositiveButton("Hinzufügen", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                alert.dismiss();
    
    
                                Snackbar.make(findViewById(R.id.fab), value, Snackbar.LENGTH_LONG)
                                        .setAction("Action", null).show();
                            }
                        });
    

    Let me know if this works, if not , Im here to keep trying until we get the solution! ;)

    P.S I updated my answer but I am not completely sure. Take a Look to the next example:

    LayoutInflater li = LayoutInflater.from(getApplicationContext()); //or context() if depends
    
    View promptsView = li.inflate(R.layout.my_dialog_layout, null);
    
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
    
    alertDialogBuilder.setView(promptsView);
    
    // set dialog message
    
    alertDialogBuilder.setTitle("My Dialog..");
    alertDialogBuilder.setIcon(R.drawable.ic_launcher);
    // create alert dialog
    final AlertDialog alertDialog = alertDialogBuilder.create();
    
    final Spinner mSpinner= (Spinner) promptsView
            .findViewById(R.id.mySpinner);
    final Button mButton = (Button) promptsView
            .findViewById(R.id.myButton);
    
    // reference UI elements from my_dialog_layout in similar fashion
    
    mSpinner.setOnItemSelectedListener(new OnSpinnerItemClicked());
    
    // show it
    alertDialog.show();
    

    Also you can be missing alert.show , so when you clicked the Fab button it does not anything because you have never sent the AlertDialog.