如何在AlertDialog.builder中使用Spinner?

3

更新:
嗨,我正在使用AlertDialog.Builder中的下拉列表(spinner)来显示可选项。但是当未点击时,spinner仅显示String array的第一项。如果点击,则会强制关闭。我的代码如下。

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

我的输出:

输入图像说明

如果我点击这个旋转器,会出现强制关闭。

任何帮助都将不胜感激,谢谢!


1
尝试在声明LayoutInflator时使用ClassName.this,而不是使用getApplicationContext()。 - Hiral Vadodaria
1
嗨,我已经更新了问题,请也查看一下。 - Pattabi Raman
谢谢,我已经按照您的建议解决了这个问题。 - Pattabi Raman
3个回答

2
尝试在声明LayoutInflator时使用ClassName.this而不是getApplicationContext()。

1

我认为您在这里使用了不恰当的上下文,尝试使用正确的上下文。

LayoutInflater factory = LayoutInflater.from(getApplicationContext());

不要使用 getApplicationContext(),尝试使用 Activity_name.this 或 getParent();


0

在此之前,builder.setView(v); 被调用了两次以移除不需要的一个。

错误可能出现在 new MyOnItemSelectedListener() 或者你设置 entries 的 xml 中。

请分享代码和错误日志。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接