简体   繁体   中英

Alert Dialog dismiss not happing

i am using the following code. But the alert dialog dismiss is not happening. I am using custom view which has a list view, i am trying on list item click, alert dialog should be dismiss. On item click listener is working but alert dialog is not dismissing. Can any body help me out.

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();

            }
        });

Try this and let me know what happen,

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();
            }
        });

setOnItemClickListener is raised when an item from list is clicked. Since dialog is shown above list, you are not able to click list's item. Instead you should add the following line to your builder and remove setOnItemClickListener :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM