Glory to Russia
Glory to Russia

Reputation: 18720

How to react to spinner item selection in an AlertDialog.Builder-generated dialog?

In my application, I display a dialog box with the following code:

final AlertDialog.Builder builder = new AlertDialog.Builder(
        getActivity());
final View myDialogForm = getActivity().getLayoutInflater()
        .inflate(R.layout.my_dialog, null);

builder.setTitle(R.string.addCompanyDialogTitle)
    .setView(myDialogForm)
    .create()
    .show();

It contains a Spinner and a TextView. When the user selects something in the spinner, the text view must be updated.

How can I implement this (react to spinner selection in a dialog box generated by AlertDialog.Builder) ?

Upvotes: 0

Views: 1389

Answers (2)

Glory to Russia
Glory to Russia

Reputation: 18720

I solved the problem in this way:

1) Modify the code above like this:

final AlertDialog.Builder builder = new AlertDialog.Builder(
        getActivity());
final View myDialogForm = getActivity().getLayoutInflater()
        .inflate(R.layout.my_dialog, null);

final Spinner mySpinner = 
    (Spinner) addCompanyDialogForm.findViewById(R.id.spinner_id);

finalOutputSpinner.setOnItemSelectedListener(this);

builder.setTitle(R.string.addCompanyDialogTitle)
    .setView(myDialogForm)
    .create()
    .show();

2) Make the class implement the interface AdapterView.OnItemSelectedListener.

3) Implement methods of that interface in that class:

@Override
public void onItemSelected(final AdapterView<?> aParent, final View aView,
        final int aPosition, final long aRowId) {
    // aRowId contains the index of the selected item
}

@Override
public void onNothingSelected(final AdapterView<?> aParent) {
    // Nothing is selected
}

Upvotes: 0

AlexBcn
AlexBcn

Reputation: 2460

You can iterate trough the views of your dialog using a recursive function such as:

    Dialog di = builder.setTitle(R.string.addCompanyDialogTitle).setView(myDialogForm).create();
    updateMessage(di.getWindow().getDecorView());
    di.show();
    //...
    private void updateMessage(View parent){
     if(parent instanceof ViewGroup){
        ViewGroup vg = (ViewGroup) parent;
        for(int i=0; i < vg.getChildCount();i++){
            View v = vg.getChildAt(i); 
            if(v instanceof RelativeLayout){
                RelativeLayout rl = (RelativeLayout) v;
                TextView tv = (TextView) rl.getChildAt(1);
                Spinner sp = (Spinner) rl.getChildAt(0);
                sp.setOnItemSelectedListener( 
                                        //..
                                        tv.setText(sp_value); 
                                        //..
                                    )
                break;
            } else {
                updateMessage(v);
            }
        }                       
    }
   }

You can control when you are on your R.layout.my_dialog with the help of instanceOf your_root_view, in my example it is a RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android">

    <Spinner ... />

    <TextView ... />

 </RelativeLayout>

Reference

Upvotes: 1

Related Questions