1

How can I have a drop down spinner inside of a popup window? I've tried looking, but the only answers I have found say to create a dialog spinner instead (which is not what I want). I tried doing it with a custom adapter and seem to be getting an error do to context (basically, once the popup window initiates, the context for my activity is gone) so how can I get context from the pop-up window or how can I implement a drop down spinner?

edit:

Adapter:

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;
    }
}

Starting Spinner in Pop-Up Window:

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();


    }
}

Error Message:

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)
JDDDDDD
  • 11
  • 2
  • Tell us more about your approach, share the details of your code, how did you try to create the the spinner inside the popup window? Share your debug or error message and please provide more details. – Daniel C. Aug 15 '17 at 00:36
  • @DanielC. I updated my post with my code – JDDDDDD Aug 15 '17 at 00:55
  • Before initiatePopupWindow() invocation could you validate if the Activity is not finished jet? Take a look to this related post: https://stackoverflow.com/questions/7811993/error-binderproxy45d459c0-is-not-valid-is-your-activity-running also to this blog http://dimitar.me/android-displaying-dialogs-from-background-threads/ – Daniel C. Aug 15 '17 at 02:39

0 Answers0