Tunandroid
Tunandroid

Reputation: 21

How can I close or dismiss from an AlertDialog?

I have put a ListView into a AlertDialog and I want to close the AlertDialog when I click on the ListView items; how can I do it?

public void createSearchDialog(final String[] Memo){
    LayoutInflater factory = LayoutInflater.from(this);
    View searchView = factory.inflate(R.layout.seach_dialog, null);

    lv = (ListView) searchView.findViewById(R.id.search_list);
    lv.setAdapter(new MyPerformanceArrayAdapter(this, Memo, memo_PW));
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
            // TODO Auto-generated method stub

                     /// close dialog
        }
    });

    searchBuilder = new AlertDialog.Builder(this);
    searchBuilder.setTitle("Search")
           .setView(searchView)
           .setNegativeButton("Back", new DialogInterface.OnClickListener() {               
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        })
           .show();     
}

Upvotes: 1

Views: 1338

Answers (2)

MikeIsrael
MikeIsrael

Reputation: 2889

just grab a pointer to your alertdialog like this

AlertDialog myDialog = searchBuilder.setTitle("Search")
           .setView(searchView)
           .setNegativeButton("Back", new DialogInterface.OnClickListener() {               
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub

            }
        }).create();

then later on you can dismiss by calling

myDialog.dismiss() 

Upvotes: 1

Pavankumar Vijapur
Pavankumar Vijapur

Reputation: 1206

And from using the app context you can dismiss is by calling dimiss on it.

Application.getProgressDialog().dismiss();

This has to handled in the list view's onItemClick()

Upvotes: 1

Related Questions