android - Drop-Down Spinner inside of Pop-Up Window -
how can have drop down spinner inside of popup window? i've tried looking, answers have found create dialog spinner instead (which not want). tried doing custom adapter , seem getting error context (basically, once popup window initiates, context activity gone) how can context pop-up window or how can implement drop down spinner?
edit:
adapter:
public class spinadapter extends arrayadapter<contact>{ // sent context private context context; // custom values 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; } // , "magic" goes here // "passive" state of spinner @override public view getview(int position, view convertview, viewgroup parent) { // created dynamic textview here, can reference own custom layout each spinner item textview label = new textview(context); label.settextcolor(color.black); // can current item using values array (users array) , current position // can reference each method has created in bean object (user class) label.settext(values.get(position).tostring()); // , return dynamic (or custom) view each spinner item return label; } // , here when "chooser" popped // same view, can customize if 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 = 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 = (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 = 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 add window -- token android.view.viewrootimpl$w@5c0ba7c not valid; activity running? @ android.view.viewrootimpl.setview(viewrootimpl.java:679) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:342) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:93) @ android.widget.popupwindow.invokepopup(popupwindow.java:1378) @ android.widget.popupwindow.showasdropdown(popupwindow.java:1234) @ android.widget.listpopupwindow.show(listpopupwindow.java:671) @ android.widget.spinner$dropdownpopup.show(spinner.java:1235) @ android.widget.spinner.performclick(spinner.java:770) @ android.support.v7.widget.appcompatspinner.performclick(appcompatspinner.java:438) @ android.view.view$performclick.run(view.java:22429) @ android.os.handler.handlecallback(handler.java:751) @ android.os.handler.dispatchmessage(handler.java:95) @ android.os.looper.loop(looper.java:154) @ android.app.activitythread.main(activitythread.java:6119) @ java.lang.reflect.method.invoke(native method) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:886) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:776)
Comments
Post a Comment