我可以在列表单击时关闭警报对话框吗?

时间:2012-04-11 13:06:10

标签: android android-alertdialog

我有一个警告对话框,上面有一个列表,我想关闭onlistclick是否可能?

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

    final String[] Categories = SQLiteHelper.getAllCategories();//this is where i get the array for my list
    ListView myList = new ListView(this);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.alert_dialog_list_view, Categories);
    myList.setAdapter(adapter);
    myList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
                     //doing something in here and then close
         }
    });
    builder.setTitle("Please Choose");
    builder.setInverseBackgroundForced(true);
    builder.setView(myList);
    final Dialog dialog = builder.create();
    dialog.show();
    }

警报对话框运行完美,我只是不想在其中放置任何按钮。

2 个答案:

答案 0 :(得分:12)

如果您在 onItemClickListener之后定义Dialog ,则只需在dialog.dismiss();方法中调用onItemClick()

答案 1 :(得分:2)

检查以下代码

 myList.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {

              //doing something in here and then close
              dialog.dismiss();
         }
    });