java-如何在AlertDialog.builder中使用Spinner?

java-如何在AlertDialog.builder中使用Spinner?,第1张

概述更新:嗨,我在AlertDialog.Builder中使用微调器以显示要选择的选项列表.但是微调器在未单击时仅显示String数组的第一项.如果单击,将显示强制关闭.我的代码如下.AlertDialog.Builderbuilder=newAlertDialog.Builder(this);LayoutInflaterli=LayoutInflater.from(this);View

更新:
嗨,我在AlertDialog.Builder中使用微调器以显示要选择的选项列表.但是微调器在未单击时仅显示String数组的第一项.如果单击,将显示强制关闭.我的代码如下.

AlertDialog.Builder builder = new AlertDialog.Builder(this);LayoutInflater li=LayoutInflater.from(this);VIEw v=li.inflate(R.layout.searchme, null);builder.setIcon(androID.R.drawable.ic_input_get);builder.setVIEw(v);builder.setTitle("Search");LayoutInflater factory = LayoutInflater.from(getApplicationContext());final VIEw textEntryVIEw = factory.inflate(R.layout.searchme, null);builder.setVIEw(textEntryVIEw);Spinner spin=(Spinner)textEntryVIEw.findVIEwByID(R.ID.searchspinner);    UtilitIEs.ManageDeptSpinner(this, spin);    for(int i=0;i<spin.getCount();i++)    {        long ID=spin.getItemIDAtposition(i);            spin.setSelection(i, true);            break;    }    spin.setonItemSelectedListener(new MyOnItemSelectedListener());    builder.setPositivebutton("Go",new DialogInterface.OnClickListener()     {               public voID onClick(DialogInterface dialog, int ID)         {            try            {                Titletext = (EditText) textEntryVIEw.findVIEwByID(R.ID.TitleText1);                persontext = (EditText) textEntryVIEw.findVIEwByID(R.ID.personText2);                prioritytext = (EditText) textEntryVIEw.findVIEwByID(R.ID.priorityText3);                Title_text = Titletext.getText().toString();                person_text = persontext.getText().toString();                priority_text = prioritytext.getText().toString();                String condition = "Titlee='"+Title_text+"' or pname ='"+person_text+"' or prior='"+priority_text+"'";                refresh_data(" ASC","prior",condition);            }            catch(Exception e)            {                e.printstacktrace();                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_SHORT).show();            }    }});    builder.setNegativebutton("Cancel",new DialogInterface.OnClickListener()     {               public voID onClick(DialogInterface dialog, int ID)         {            dialog.cancel();                // Do nothing        }       });    AlertDialog alert = builder.create();    alert.show();

MyOnItemSelectedListener.java:

public class MyOnItemSelectedListener implements OnItemSelectedListener {    @SuppressWarnings("unused")    public voID onItemSelected(AdapterVIEw<?> parent,VIEw vIEw, int pos, long ID)     {    try        {        switch(parent.getID())        {            case R.ID.searchspinner:                Toast.makeText(getApplicationContext(),"\n Selected : "+parent.getItemAtposition(pos).toString()+"\n",Toast.LENGTH_LONG).show();                break;        }        }catch(Exception e)        {            e.printstacktrace();        }    }    public voID onnothingSelected(AdapterVIEw<?> parent)     {            //  Do nothing.    }}

我得到如下异常:

androID.vIEw.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

我的输出:

如果单击此微调器,则将强制关闭.

非常感谢任何帮助,并在此先感谢

解决方法:

在声明LayoutInflator时,请尝试使用Classname.this而不是使用getApplicationContext().

总结

以上是内存溢出为你收集整理的java-如何在AlertDialog.builder中使用Spinner?全部内容,希望文章能够帮你解决java-如何在AlertDialog.builder中使用Spinner?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1072950.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存