AlertDialog - setTitle ok,setMessage ok,两者都出错

AlertDialog - setTitle okay, setMessage okay, both error

从我的 activity 我启动了第一个带有 textView 的 alertDialog,以从用户那里获取名称。 之后打开第二个 alertDialog。它有一个 listView,用户必须选择其项目 2,3 或 4 之一。

我想为两个 alertDialogs 设置标题和消息。但似乎我只能设置其中一个。如果我尝试在第二个 alertDialog 上设置我的 listView 保持为空。

这是我的代码:

final AlertDialog.Builder alert = new AlertDialog.Builder(this);
        final AlertDialog.Builder builder = new AlertDialog.Builder(this );

        builder.setTitle("Amount of answers");
       // builder.setMessage("Please choose the amount of answers for your question.");
        ArrayList<Integer> myArrayList= new ArrayList<Integer>();
        for(Integer i=2;i<=4;i++){
            myArrayList.add(i);
        }
        ListAdapter myAdapter = new ArrayAdapter<Integer>(getBaseContext(), R.layout.support_simple_spinner_dropdown_item,myArrayList);
        builder.setAdapter(myAdapter, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if(which==0){
                    startView.amountOfAnswers = 2;
                }
                if(which==1){
                    startView.amountOfAnswers = 3;
                }
                if(which==2){
                    startView.amountOfAnswers = 4;
                }
                Intent myIntent = new Intent(getBaseContext(),editQuestion.class);
                Bundle myBundle = new Bundle();
                myBundle.putString("fill","false");
                myIntent.putExtras(myBundle);
                startActivity(myIntent);
            }
        });


        alert.setTitle("Name of question");
        //alert.setMessage("Please enter the name of your question.");
        final EditText input = new EditText(this);
        alert.setView(input);
       alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                Editable value = input.getText();
                startView.questionName=value.toString();
                startView.questionPath=startView.quizPath + "/" + startView.questionName;
                builder.show();
            }
        });
       alert.show();

当我删除 setMessage 方法之前的 // 时,我的 listView 为空。

你看到我的错误了吗?

你试过这样吗。这是一个包含标题和消息的工作代码

private AlertDialog.Builder build;
build = new AlertDialog.Builder(Activity.this);
        build.setTitle("Delete");
        build.setMessage("Do you want to delete ?");
        build.setPositiveButton("Yes", new DialogInterface.OnClickListener() 
        {
            public void onClick(DialogInterface dialog, int which)
            {
            }
        });

            build.setNegativeButton("No", new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dialog, int which) 
                {
                    dialog.cancel();
                    }
                });
            AlertDialog alert = build.create();
            alert.show();
            return true;
        }

希望对您有所帮助...

您不能在同一警报上调用 setMessage 和 setAdapter AlertDialog.Builder 它们都会更改对话框的内容区域。

public AlertDialog.Builder setAdapter (ListAdapter adapter, DialogInterface.OnClickListener listener)

Added in API level 1 Set a list of items, which are supplied by the given ListAdapter, to be displayed in the dialog as the content, you will be notified of the selected item via the supplied listener.

这就是当您调用 setMessage

时列表为空的原因

所以您需要做的是为第二个对话框创建一个布局,其中包含一个 TextView 和一个 ListView,并使用

将其设置为 AlertDialog.Builder 的视图

public AlertDialog.Builder setView (View view)