Android在对话框中使用Spinner

时间:2014-10-26 15:17:14

标签: android android-spinner android-alertdialog

在我的应用程序中,我有一个AlertDialog带有Spinner小部件,我想填写任何项目。但我得到这个错误:

Error:(100, 52) java: no suitable constructor found for ArrayAdapter(<anonymous android.view.View.OnClickListener>,int,java.util.List<java.lang.String>)
    constructor android.widget.ArrayAdapter.ArrayAdapter(android.content.Context,int,int,java.util.List<java.lang.String>) is not applicable
      (actual and formal argument lists differ in length)
    constructor android.widget.ArrayAdapter.ArrayAdapter(android.content.Context,int,java.util.List<java.lang.String>) is not applicable
      (actual argument <anonymous android.view.View.OnClickListener> cannot be converted to android.content.Context by method invocation conversion)
    constructor android.widget.ArrayAdapter.ArrayAdapter(android.content.Context,int,int,java.lang.String[]) is not applicable
      (actual and formal argument lists differ in length)
    constructor android.widget.ArrayAdapter.ArrayAdapter(android.content.Context,int,java.lang.String[]) is not applicable
      (actual argument <anonymous android.view.View.OnClickListener> cannot be converted to android.content.Context by method invocation conversion)
    constructor android.widget.ArrayAdapter.ArrayAdapter(android.content.Context,int,int) is not applicable
      (actual argument <anonymous android.view.View.OnClickListener> cannot be converted to android.content.Context by method invocation conversion)
    constructor android.widget.ArrayAdapter.ArrayAdapter(android.content.Context,int) is not applicable
      (actual and formal argument lists differ in length)

我的问题是在Spinner

中附加AlertDialog

enter image description here

Dialog xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent" android:id="@+id/layoutRoot" android:background="@null">

   <LinearLayout
                android:orientation="horizontal"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
            <Spinner
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:id="@+id/spinner" android:spinnerMode="dropdown" android:layout_weight="0.80"/>
            <TextView
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:text="New Text"
                    android:id="@+id/textView4" android:layout_weight="0.20"/>
        </LinearLayout>

</LinearLayout>

通过单击“活动”中的类扩展来填充活动。

 bt_send_group.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(ContactListActivity.this);
                View v = G.inflater.inflate(R.layout.group_send_sms, null);
                builder.setView(v);
                final AlertDialog sms_dialog = builder.create();
                sms_dialog.show();

                Spinner panel_numbers = (Spinner) v.findViewById ( R.id.spinner );
                List<String> list = new ArrayList<String>();
                list.add("Android");
                list.add("Java");
                list.add("Spinner Data");
                list.add("Spinner Adapter");
                list.add("Spinner Example");

                ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                        (this, android.R.layout.simple_spinner_item,list);

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

                panel_numbers.setAdapter(dataAdapter);
            }
        });

1 个答案:

答案 0 :(得分:0)

替换代码的这一行

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                    (this, android.R.layout.simple_spinner_item,list);

这一行

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>
                    (ContactListActivity.this, android.R.layout.simple_spinner_item,list);