简体   繁体   中英

Android - spinner within dialog won't populate

I'm trying to add a two spinners inside a dialog (popup). The problem I'm having is populating the spinners. I get no error I can see, and basically the same code works if It's in a tab-fragment, and not the dialog.

This is the code that does not popluate the spinners inside the dialog.

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    LayoutInflater inflater = getActivity().getLayoutInflater();

    League league;

    league = ((LeagueMainActivity)getActivity()).getLeague();

    View v = inflater.inflate(R.layout.diaglog_add_match, null);

    Spinner spinner1 = (Spinner) v.findViewById(R.id.spinner_dialog_player1);
    Spinner spinner2 = (Spinner) v.findViewById(R.id.spinner_dialog_player2);

    String [] items = {"test 1", "test 2"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, items);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    Log.d("Spinner: ", "" + spinner1);

    spinner1.setAdapter(adapter);
    spinner2.setAdapter(adapter);

    builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))
            .setTitle("Add match")
            .setPositiveButton("Create", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int id) {
                    // sign in the user ...
                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    //LoginDialogFragment.this.getDialog().cancel();
                    /* do I really need to do anything??? */
                }
            });

    AlertDialog dialog = builder.create();
    return dialog;
}

This is the code that works within a (tabbed) fragment:

public class UnnamedFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_unnamed, container, false);

    Spinner spinner1 = (Spinner) rootView.findViewById(R.id.spinner);

    String [] items = {"test 1", "test 2"};

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, items);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    Log.d("Spinner: ", "" + spinner1);

    spinner1.setAdapter(adapter);

    return rootView;
}

}

Okay so what I did wrong was inflating/creating two independent views with the same context.

First I did:

 View v = inflater.inflate(R.layout.diaglog_add_match, null);

And then:

builder.setView(inflater.inflate(R.layout.diaglog_add_match, null))

So I set the view for the builder as a new view, and not the same ones I used for the spinner. So instead if I do:

View v = inflater.inflate(R.layout.diaglog_add_match, null);
builder.setView(v)

That does the trick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM