2

我的DialogFragment中有下一个代码:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Log.i(getClass().getName(), "onCreateDialog");
    final FileSystemAdapter adapter = getAdapter();
    adapter.showFS(null);

    AlertDialog b = new AlertDialog.Builder(getActivity())
            .setTitle(title)
            .setCancelable(false)
            .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //some actions
                }
            })
            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dismiss();
                }
            })
            .setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    adapter.showFS(adapter.getItem(i));
                }
            })
            .create();
    return b;
}

单击列表元素后,调用最后一个onClick方法,然后关闭对话框。


如何防止这种行为并保持对话框打开,直到调用解除方法?

4

1 回答 1

7

您可以创建一个新的 ListView 并将其设置为 AlertDialog.Builder 中的自定义视图,并将您的 OnClickListener 代码放入 listView 中的 OnItemClickListener 中。尝试这样的事情:

ListView listView = new ListView(this);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new ListView.OnItemClickListener() {
    public void onItemClick(AdapterView<?> listView, View itemView, int position, long itemId) {
         adapter.showFS(adapter.getItem(position));
    }
});

AlertDialog b = new AlertDialog.Builder(getActivity())
        .setTitle(title)
        .setCancelable(false)
        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                //some actions
            }
        })
        .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dismiss();
            }
        })
        .setView(listView)
        .create();
return b;
于 2013-06-23T16:18:27.483 回答