Android:无法关闭自定义对话框

时间:2013-11-21 20:16:40

标签: android android-alertdialog android-gridview

我有一个带有gridview的自定义对话框。 单击文本视图(运行)时会弹出对话框。对话框中有一个gridview。 我的目标是在从gridview中选择值时更改运行textview的值并关闭对话框

    protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    switch (id) {
    case CATEGORY_ID:

        AlertDialog.Builder builder;
        AlertDialog alertDialog;
        // Context mContext = this;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.gridview_cell,
                (ViewGroup) findViewById(R.id.layout_root));

        GridView gridview = (GridView) layout.findViewById(R.id.gridView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.text_gridview, R.id.run_cell, numbers);

        gridview.setAdapter(adapter);

        builder = new AlertDialog.Builder(context);
        builder.setView(layout);
        dialog = builder.create();
        gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

                runs.setText(String.valueOf(position));
       **dialog.cancel() or dialog.dismiss are giving me this error: 'Cannot refer to a non-final variable result inside an inner class defined in a different method'**
            }
        });
        break;

    }
    return dialog;
}

1 个答案:

答案 0 :(得分:2)

而不是

Dialog dialog = null;

待办事项

final Dialog dialog;

要更好地解释此问题,请检查this previously asked question

修改

我没有看到问题的其余部分。看到您有switch语句,您可以使用default设置值,如下所示:

protected Dialog onCreateDialog(int id) {
    final Dialog dialog;
    switch (id) {
        case CATEGORY_ID: 
           //your code
           break;
        default:
           dialog = null;
           break;
    }
    return dialog;
}