弹出窗口内的下拉旋转器

时间:2017-08-15 00:19:48

标签: android android-studio

如何在弹出窗口中放置一个下拉微调器?我试过看,但我找到的唯一答案是创建一个对话框微调器(这不是我想要的)。我尝试使用自定义适配器执行此操作,并且似乎对上下文执行了错误操作(基本上,一旦弹出窗口启动,我的活动的上下文消失了)所以如何从弹出窗口获取上下文或怎么能我实现了一个下拉式微调器?

编辑:

适配器:

public class SpinAdapter extends ArrayAdapter<contact>{

    // Your sent context
    private Context context;
    // Your custom values for the spinner (User)
    private ArrayList<contact> values;

    public SpinAdapter(Context context, int textViewResourceId,
                       ArrayList<contact> values) {
        super(context, textViewResourceId, values);
        this.context = context;
        this.values = values;
    }

    public int getCount(){
        return values.size();
    }

    public contact getItem(int position){
        return values.get(position);
    }

    public long getItemId(int position){
        return position;
    }


    // And the "magic" goes here
    // This is for the "passive" state of the spinner
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // I created a dynamic TextView here, but you can reference your own  custom layout for each spinner item
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        // Then you can get the current item using the values array (Users array) and the current position
        // You can NOW reference each method you has created in your bean object (User class)
        label.setText(values.get(position).toString());

        // And finally return your dynamic (or custom) view for each spinner item
        return label;
    }

    // And here is when the "chooser" is popped up
    // Normally is the same view, but you can customize it if you want
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        TextView label = new TextView(context);
        label.setTextColor(Color.BLACK);
        label.setText(values.get(position).toString());

        return label;
    }
}

在弹出窗口中启动微调器:

private void initiatePopupWindow(){
    try{
        ArrayList<contact> Con = tinydb.getListAdd("address", contact.class);
        contact a = new contact("a","b");
        Con.add(a);
        SpinAdapter adapt;
        adapt = new SpinAdapter(todoroom.this ,
                android.R.layout.simple_spinner_item,
                Con);
        LayoutInflater inflater = (LayoutInflater) todoroom.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.addpop,
                (ViewGroup) findViewById(android.R.id.background));
        pw = new PopupWindow(layout,300,300,true);
        //pw.isTouchable();
       // pw.isFocusable();
        pw.showAtLocation(layout, Gravity.CENTER, 0,0);

        //final EditText address = (EditText)layout.findViewById(R.id.editAddress) ;
        final Spinner address = (Spinner) layout.findViewById(R.id.spinner);
        final EditText name = (EditText)layout.findViewById(R.id.editName);

        address.setAdapter(adapt);


        ImageButton back = (ImageButton)layout.findViewById(R.id.cancelBut);
        back.setOnClickListener(
                new View.OnClickListener(){
                   public void onClick(View view){
                        pw.dismiss();
                    }


        }
        );

        Button add = (Button)layout.findViewById(R.id.addBut);
        add.setOnClickListener(
                new View.OnClickListener(){
                  public void onClick(View view){
                      task a = new task(name.getText().toString(), address.getSelectedItem().toString() );
                      todos.add(a);
                      tinydb.putListTask("tasks",(ArrayList)todos);
                      pw.dismiss();
                      list.setAdapter(adapter);

                  }
                }
        );



    } catch (Exception e){
        e.printStackTrace();


    }
}

错误讯息:

Process: com.example.vlad.gpslocate, PID: 27527
                                                                        android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@5c0ba7c is not valid; is your activity running?
                                                                            at android.view.ViewRootImpl.setView(ViewRootImpl.java:679)
                                                                            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:342)
                                                                            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
                                                                            at android.widget.PopupWindow.invokePopup(PopupWindow.java:1378)
                                                                            at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:1234)
                                                                            at android.widget.ListPopupWindow.show(ListPopupWindow.java:671)
                                                                            at android.widget.Spinner$DropdownPopup.show(Spinner.java:1235)
                                                                            at android.widget.Spinner.performClick(Spinner.java:770)
                                                                            at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:438)
                                                                            at android.view.View$PerformClick.run(View.java:22429)
                                                                            at android.os.Handler.handleCallback(Handler.java:751)
                                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                            at android.os.Looper.loop(Looper.java:154)
                                                                            at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                                            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

0 个答案:

没有答案