Spinner exception resolution in Android Pop-up windows

Source: Internet
Author: User

Pop-up windows commonly used Alertdialog socks, because Alertdialog can easily add the items you want. such as the OK/Cancel button, the custom title bar, custom information layout and so on. When customizing layouts, for example, we use LinearLayout to lay out, and then add items to the linearlayout, such as TextView, EditText, and so on. These are easy to implement.

However, if you add spinner, there may be a problem. Looking for a long time, the discovery is caused by the context. Because spinner needs to rely on the form when it clicks to expand the drop-down list, using the context simply provides a contextual and does not provide a form, so it can cause errors. The following is the implemented code:

Package Com.sparkle;import Android.app.activity;import Android.app.alertdialog;import Android.app.alertdialog.builder;import Android.app.dialog;import Android.content.context;import Android.content.dialoginterface;import Android.graphics.color;import Android.util.log;import Android.view.View; Import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.linearlayout;import Android.widget.spinner;import Android.widget.spinneradapter;import Android.widget.textview;public class Contury { Private activity _activity = null;private Context _context = null;private Spinner _spinner = null;public Contury (activity Activity) {_activity = Activity;_context = activity;} Private LinearLayout generatelinearlayout (int orientation) {LinearLayout linearlayout = new LinearLayout (_context); Linearlayout.setpadding (+, +, +); linearlayout.setorientation (orientation); return linearlayout;} @SuppressWarnings ("deprecation") private Spinner Generatespinner () {Spinner Spinner = new Spinner(_activity); Linearlayout.layoutparams layoutparams = new Linearlayout.layoutparams (LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); Spinner.setlayoutparams (layoutparams); return spinner;} public void Popup () {Builder alertdialogbuilder = new Alertdialog.builder (_activity); View view = Generatelayout (); Alertdialogbuilder.setview (view); Alertdialogbuilder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) {LOG.E ("TEST", "OK" );}}); Alertdialogbuilder.setnegativebutton ("Cancel", null);D ialog alertdialog = Alertdialogbuilder.create (); Alertdialog.show ();} Private View Generatelayout () {LinearLayout linearlayout = generatelinearlayout (linearlayout.vertical); LinearLayout messagelinearlayout = Generatemessagelayout (); Linearlayout.addview (messagelinearlayout); return LinearLayout;} Private LinearLayout Generatemessagelayout () {LinearLayout linearlayout = generatelinearlayout ( Linearlayout.horizontal); _Spinner = Generatespinner (); _spinner.setadapter (new Cityadapter (_activity)); _spinner.setselection (0); Linearlayout.addview (_spinner); return linearlayout;} Private class Cityadapter extends Baseadapter implements Spinneradapter {private Context _con=null;private string[] _citi Es=null;public Cityadapter (Context context) {_con=context;_cities=_con.getresources (). Getstringarray (        r.array.cities);}        @Override public int GetCount () {if (_cities==null) {return 0;        } return _cities.length; } @Override public Object getItem (int position) {if (_cities==null| |        _cities.length<=0) {return null;        } return _cities[position];        } @Override public long getitemid (int position) {return position; } @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {TextView textview=new TextView (_con); te Xtview.settextcolor (color.black); textview.setTextSize, Textview.settext (_cities[position]); return textView;} }}

1, spinner and alertdialog new need to use activity, using the context will be in the popup drop-down menu caused errors.

2, _con.getresources (). Getstringarray (R.array.cities) is an array defined in the load XML.

City.xml (in the Values folder)

<?xml version= "1.0" encoding= "Utf-8"?><resources>    <string-array name= "Cities" >        <item > Xiamen </item>        <item> Beijing </item>        <item> Shanghai </item>    </string-array> </resources>

Spinner exception resolution in Android Pop-up windows

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.