user7869779
user7869779

Reputation:

How to close a dialog with a listview

I have an OnClickListener that opens a dialog with a listview in it and a cancel button, the cancel button closes the dialog naturally, but I also want to close the dialog after an item on listview is clicked and some task done,but I can't figure out how, here is my code:

View.OnClickListener clickListener = new View.OnClickListener()
{
    TextView textView;

    @Override
    public void onClick(View view)
    {
        textView = (TextView) view;
        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(AttendanceStatsActivity.this);
        LayoutInflater inflater = getLayoutInflater();
        View convertView = inflater.inflate(R.layout.list_dialog, null);
        alertDialog.setView(convertView);
        alertDialog.setTitle("Сурагчид");

        alertDialog.setNegativeButton("Хаах", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                dialog.dismiss();
            }
        });

        ListView lv = (ListView) convertView.findViewById(R.id.lv);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(getApplicationContext(), R.layout.my_spinner_dropdown_item,
                studentNames);
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
            {
                String fullName = studentNames.get(i);
                textView.setText(fullName);
            }
        });
        alertDialog.show();
    }
};

I can't simply call dialog.dismiss() since it's not recognized within onItemclick()

Upvotes: 0

Views: 783

Answers (3)

Junaid Fahad
Junaid Fahad

Reputation: 149

Try this:

put alertDialog.dismiss() inside lv.setOnItemClickListener if your task is done and remove dialog.dismiss(); from alertDialog.setNegativeButton.

Upvotes: 0

Kingfisher Phuoc
Kingfisher Phuoc

Reputation: 8200

You need to place your show method before the onItemClickListener:

final AlertDialog dialog =    alertDialog.show();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
    {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
        {
            String fullName = studentNames.get(i);
            textView.setText(fullName);
            dialog.dismiss();
        }
    });

Upvotes: 2

Quang Nguyen
Quang Nguyen

Reputation: 2660

You need to declare dialog variable first.

//... above code
alertDialog.setTitle("Сурагчид");
// Get dialog here.
AlertDialog dialog = alertDialog.create();

// Now you can use 
 lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
 {
      @Override
      public void onItemClick(AdapterView<?> adapterView, View view, int i, long l)
      {
         //Use it here. It is ok now
         dialog.dismiss();
       }
 });

Upvotes: 0

Related Questions