从我的活动中我开始第一个带有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为空。
你看到我的错误吗?
答案 0 :(得分:0)
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;
}
希望它有所帮助...
答案 1 :(得分:0)
您无法在相同警报AlertDialog.Builder上调用setMessage和setAdapter,这些都会更改对话框的内容区域。
public AlertDialog.Builder setAdapter(ListAdapter adapter,DialogInterface.OnClickListener listener)
在API级别1中添加 设置由给定ListAdapter提供的项目列表,作为内容在对话框中显示,您将通过提供的监听器通知您所选项目。
这就是您拨打setMessage
所以你需要做的是为第二个对话框创建一个布局,其中包含TextView和ListView,并使用
将其设置为AlertDialog.Builder的视图public AlertDialog.Builder setView(View view)