简体   繁体   English

Android-对话框中的微调框不会填充

[英]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. 这样就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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