Alertdialog解雇没有发生

时间:2012-04-19 09:16:16

标签: android

我正在使用以下代码。但警报对话框没有发生。我正在使用具有列表视图的自定义视图,我正在尝试列表项单击,警告对话框应该被忽略。项目单击侦听器正在运行但警报对话框未解除。任何人都可以帮助我。

View titleView = new View(MixContainerInfoActivity.this);
    titleView = (View)getLayoutInflater().inflate(R.layout.custom_unit_title, null);
    TextView tv = (TextView)titleView.findViewById(R.id.custom_title_txt_view);     
    tv.setTypeface(typeFace);
    tv.setText("CONTAINER OR IMPLEMENT...");

    View view = new View(MixContainerInfoActivity.this);
    view = (View)getLayoutInflater().inflate(R.layout.unit_list, null);
    containerList = (ListView)view.findViewById(R.id.unit_list_view);
    ArrayAdapter<Vector> containerAdapter = new UnitMenuAdapter(MixContainerInfoActivity.this, R.layout.custom_unit_list, containerVector);     
    containerList.setAdapter(containerAdapter);


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

     builder.setCustomTitle(titleView);
     builder.setView(view);



     final AlertDialog alert = builder.create();
     alert.show(); //edited here

     containerList.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                System.out.println("Item Clicked..");

                alert.dismiss();

            }
        });

2 个答案:

答案 0 :(得分:2)

试试这个,让我知道会发生什么,

AlertDialog.Builder builder = new AlertDialog.Builder(MixContainerInfoActivity.this);
View view = new View(MixContainerInfoActivity.this);
view = (View)getLayoutInflater().inflate(R.layout.unit_list, null);
builder.setCustomTitle(titleView);
builder.setView(view);
containerList = (ListView)view.findViewById(R.id.unit_list_view);
ArrayAdapter<Vector> containerAdapter = new UnitMenuAdapter(MixContainerInfoActivity.this, R.layout.custom_unit_list, containerVector);     
containerList.setAdapter(containerAdapter);
final AlertDialog alert = builder.create();
alert.show();

containerList.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                System.out.println("Item Clicked..");
                alert.dismiss();
            }
        });

答案 1 :(得分:0)

单击列表中的项目时会引发

setOnItemClickListener 。由于对话框显示在列表上方,因此您无法单击列表的项目。相反,您应该将以下行添加到构建器并删除 setOnItemClickListener

//it will dismiss the dialog automatically
builder.setNegativeButton("OK", null);