Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I close AlertDialog when I click on ListView item

Here is my dialog code, there is a ListView in a dialog, I wanna close this dialog when I click on ListView:

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

    }

I tried to put this code in onItemClick()

    searchBuilder.create().dismiss();

    searchBuilder.create().cancel();

But it's not working.

like image 229
Intathep Avatar asked Jan 22 '12 05:01

Intathep


People also ask

How do I turn off AlertDialog?

AlertDialog generally consists of the main title, the message, and two buttons, technically termed as a positive button and a negative button. Both positive and negative buttons can be programmed to perform various actions. By default, the negative button lets close the AlertDialog without any additional lines of code.

What is the difference between dialog and AlertDialog?

AlertDialog is a lightweight version of a Dialog. This is supposed to deal with INFORMATIVE matters only, That's the reason why complex interactions with the user are limited. Dialog on the other hand is able to do even more complex things .

What is the use of AlertDialog in Android?

Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.


2 Answers

Change .show() to dialog = searchBuilder.show(); then put dialog.dismiss() to onItemClick().

like image 62
Intathep Avatar answered Nov 10 '22 00:11

Intathep


Complete Code For this problem

 public void showDialog(){
    final AlertDialog.Builder dialog = new AlertDialog.Builder(getContext());
    dialog.setTitle("Choose App");
    dialog.setCancelable(true);

    View view = ((Activity)getContext()).getLayoutInflater().inflate(R.layout.custom_dialog_all_app, null);
    list = (ListView) view.findViewById(R.id.AllAppList);
    AllAppPckName = getPackages();
    AllAppListAdapter adapter= new AllAppListAdapter(getContext(), R.layout.app_item, AllAppPckName);

    list.setAdapter(adapter);

    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            appIcon.setVisibility(View.VISIBLE);
            appIcon.setImageDrawable(getPackageIcon(getContext(), AllAppPckName.get(i)));
            appNameBtn.setText(getAppNameFromPkgName(getContext(), AllAppPckName.get(i)));

            dialogg.dismiss();


        }
    });


    dialog.setView(view);
    dialogg = dialog.show();
}
like image 32
Sanjay Hadiya Avatar answered Nov 09 '22 23:11

Sanjay Hadiya