【问题标题】:How to execute action after DialogFragment positive button clicked单击 DialogFragment 正按钮后如何执行操作
【发布时间】:2013-04-04 21:53:27
【问题描述】:

我创建了以下从 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 后从列表中删除元素。

【问题讨论】:

    标签: android android-listfragment android-dialogfragment


    【解决方案1】:

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

    示例片段:

    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);
            }
        }
    
    }
    

    这是我在改变方向时也可以使用的唯一方法。

    【讨论】:

      【解决方案2】:

      您有两个选项可以从PayBillDialogFragment 调用ListFragment

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

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

      第二。您可以在创建 DialogFragment 对象时只使用DialogFragment.setTargetFragment(),然后在PayBillDialogFragment.setPositiveButton(onClick()) 中通过DialogFragment.getTargetFragment() 获取PayBillDialogFragment 并在那里调用删除方法。

      Callback to a Fragment from a DialogFragment

      【讨论】:

        【解决方案3】:

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

        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
           }
        });
        

        【讨论】:

        • 我不明白。我应该如何从 onClick 方法中删除 arraylist/arrayadapter 中的元素?
        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2022-12-02
          • 1970-01-01
          • 1970-01-01
          • 2012-01-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多