8

我创建了以下从 Android 文档派生的 DialogFragment:

公共类 PayBillDialogFragment 扩展 DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){

        final Bundle b = this.getArguments();
        // Use the Builder class for convenient dialog construction
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("Paga bollettino")
               .setPositiveButton("Paga", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // FIRE ZE MISSILES!


                   }
               })
               .setNegativeButton("Cancella", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       // User cancelled the dialog
                   }
               });
        // Create the AlertDialog object and return it
        return builder.create();

    }





}

从另一个片段(ListFragment)中,当单击列表的一行时,应该打开 DialogFragment 并且在按下 DialogFragment 的肯定按钮后,我希望能够删除 ListFragment 的选定行并调用一个方法来执行与删除相关的远程操作。我实现了 ListFragment 如下:

public static class ListFragment extends android.support.v4.app.ListFragment {



        ArrayList<String> listItems=new ArrayList<String>();


        ArrayAdapter<String> adapter;


        public static final String ARG_SECTION_NUMBER = "section_number";

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            final View rootView = inflater.inflate(R.layout.list_fragment_view,
                    container, false);


            ListView lv = (ListView)rootView.findViewById(android.R.id.list);

            }});
            adapter=new ArrayAdapter<String>(this.getActivity(),
                    android.R.layout.simple_list_item_1,
                    listItems);
                setListAdapter(adapter);
            return rootView;
        }



        @Override
        public void onListItemClick(ListView l, View v, int position, long id) {



            //opening the dialogfragment


        }


    }




    }

我不知道的是如何处理DialogFragment的正按钮点击后的动作。你能帮助我吗?

编辑:澄清一下,这是工作流程:单击列表-> 显示 DialogFragment -> 单击 DialogFragment 后从列表中删除元素。

4

4 回答 4

10

这就是我处理片段和对话片段之间的通信的方式

示例片段:

public class MainFragment extends Fragment {

    private static final int REQ_CODE = 1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.main_fragment, container, false);
        Button b = (Button) v.findViewById(R.id.button);
        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                MyDialog dialog = new MyDialog();
                dialog.setTargetFragment(MainFragment.this, REQ_CODE);
                dialog.show(getFragmentManager(), "dialog");
            }
        });
        return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Toast.makeText(getActivity(), "Result: " + resultCode,
                Toast.LENGTH_SHORT).show();
        super.onActivityResult(requestCode, resultCode, data);
    }

}

示例对话框片段:

public class MyDialog extends DialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("My dialog message")
                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        notifyToTarget(Activity.RESULT_OK);
                    }
                })
                .setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                notifyToTarget(Activity.RESULT_CANCELED);
                            }
                        });
        return builder.create();
    }

    private void notifyToTarget(int code) {
        Fragment targetFragment = getTargetFragment();
        if (targetFragment != null) {
            targetFragment.onActivityResult(getTargetRequestCode(), code, null);
        }
    }

}

这也是我在改变方向时得到的唯一方法。

于 2013-04-18T14:21:35.513 回答
2

您有两个选项可以调用ListFragmentfrom PayBillDialogFragment

一个是 Android 指南推荐的。所有通信都通过托管Activity。这就是您Activity通过调用((HostingActivity)PayBillDialogFragment.getActivity()).deleteItem()inside获得托管的方式PayBillDialogFragment.setPositiveButton(onClick())。在HostingActivity.deleteItem()获取膨胀PayBillDialogFragment并在其中调用一些删除方法。

请参阅http://developer.android.com/guide/components/fragments.html#EventCallbacks

第二。您可以DialogFragment.setTargetFragment()在创建 DialogFragment 对象时,然后在内部PayBillDialogFragment.setPositiveButton(onClick())您可以PayBillDialogFragment通过DialogFragment.getTargetFragment()并在那里调用 delete 方法。

请参阅从 DialogFragment 回调片段

于 2013-04-18T13:58:14.120 回答
0

列表片段将使用适配器来显示元素。适配器需要一些集合形式的输入。因此,您可以做的是,当用户按下对话框片段上的说确定按钮并将其传达回列表片段时,您可以从集合中删除该特定元素并再次使用修改的集合设置列表片段的适配器。

于 2014-04-25T06:46:58.000 回答
0

要调用对话框,您可以使用它:

android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
if (fm.findFragmentByTag("PayBillDialogFragment") == null) {
    PayBillDialogFragment payBillDialogFragment = new PayBillDialogFragment();
    payBillDialogFragment.show(fm, "PayBillDialogFragment");
}

在您的对话框片段中,

setPositiveButton("Paga", new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int id) {
       //Add your code here that should execute
       //when positive button is clicked
   }
});
于 2013-04-12T11:43:26.373 回答