Spinner在弹出窗口中显示为下拉?

时间:2016-07-15 07:13:07

标签: android android-spinner popupwindow

如何在弹出窗口中显示微调器作为下拉菜单,它在对话框模式下正常工作但是当我将其设置为下拉时它会给我一个错误。

这是我的代码

LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE);
 final View popupView10 =layoutInflater.inflate(R.layout.successful_dontfollow_main, null);
popupWindow10 = new    PopupWindow(popupView10,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
 popupWindow10.showAtLocation(popupView10, Gravity.CENTER, 0,0);
 popupWindow9.dismiss();
 spinner1 = (Spinner) popupView10.findViewById(R.id.spinner2);
 spinner1.setAdapter(new MycustomAdapter( MainActivity.this,R.layout.popupbutton2_activity, payment));

和我的自定义适配器类

 public  class MycustomAdapter extends ArrayAdapter<String>
{
    String[] payment={"BARCLAYS XXXX-XXX-XX89 ","BANK OF AMERICA XXXX-XXX-XX89","HSBC XXXX-XXX-XX89 "};

    public MycustomAdapter(Context context, int textViewResourceId,
                           String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

当我将其设置为Drop down时出现此错误

07-15 12:26:27.952 24452-24452/com.example.user.popup E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: com.example.user.popup, PID: 24452
                                                                    android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@228edc12 is not valid; is your activity running?
                                                                        at android.view.ViewRootImpl.setView(ViewRootImpl.java:562)
                                                                        at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:282)
                                                                        at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:85)
                                                                        at android.widget.PopupWindow.invokePopup(PopupWindow.java:1104)
                                                                        at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1008)
                                                                        at android.widget.ListPopupWindow.show(ListPopupWindow.java:639)
                                                                        at android.widget.Spinner$DropdownPopup.show(Spinner.java:1138)
                                                                        at android.widget.Spinner.performClick(Spinner.java:683)
                                                                        at android.view.View$PerformClick.run(View.java:19881)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:135)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5294)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at java.lang.reflect.Method.invoke(Method.java:372)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

如何将微调器设置为下拉?

2 个答案:

答案 0 :(得分:1)

 MainActivity.this.runOnUiThread(new Runnable() {
        public void run() {
         //Do your UI operations like dialog opening or Toast here


                  LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
                  .getSystemService(LAYOUT_INFLATER_SERVICE);
                 final View popupView10 =layoutInflater.inflate(R.layout.successful_dontfollow_main, null);
                 popupWindow10 = new    PopupWindow(popupView10,ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT,true);
                 popupWindow10.showAtLocation(popupView10, Gravity.CENTER, 0,0);
                 popupWindow9.dismiss();
                 spinner1 = (Spinner) popupView10.findViewById(R.id.spinner2);
                 spinner1.setAdapter(new MycustomAdapter( MainActivity.this,R.layout.popupbutton2_activity, payment));
               }
         });

答案 1 :(得分:0)

使用以下代码将弹出窗口显示为微调器下拉列表:

活动类:

public class SpinnerActivity extends Activity {

private LinearLayout layout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner);

    Spinner s;
    layout = (LinearLayout) findViewById(R.id.popup);
    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupWindow popUp= showDropDownData();
            popUp.showAsDropDown(view, 0, 0);
        }
    });
}

private PopupWindow showDropDownData() {
    PopupWindow popupWindow = new PopupWindow(this);

    //Initializing data from string.xml file
    String[] data={"BARCLAYS XXXX-XXX-XX89 ","BANK OF AMERICA XXXX-XXX-XX89","HSBC XXXX-XXX-XX89 "};
    CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, data);

    // the drop down list is a list view
    ListView listViewSort = new ListView(this);

    // set our adapter and pass our pop up window contents
    listViewSort.setAdapter(adapter);

    // set on item selected
    //   listViewSort.setOnItemClickListener(onItemClickListener());

    popupWindow.setFocusable(true);
    popupWindow.setWidth(layout.getWidth());
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);

    popupWindow.setContentView(listViewSort);
    return popupWindow;
}
}
下面的

是Xml文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/layout_background"
android:padding="4dp">


<ImageView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:src="@android:drawable/btn_star_big_on" />

<TextView
    android:id="@+id/openPopup"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:gravity="left|center_vertical"
    android:paddingLeft="10dp"
    android:text="popup window" />

</LinearLayout>

您可以使用onItemClickListener()从下拉列表中选择一个项目。