简体   繁体   English

如何通过列表片段的选项菜单调用DialogFragment的ListFragment和DialogFragment之间进行通信?

[英]How do I communicate between ListFragment and DialogFragment where the DialogFragment is invoked via the options menu of list fragment?

I have a class "ListFrag" implementing ListFragment and within that class, I have an nested static DialogFragment class "SortDialogFragment" which is invoked through one of the options in the options menu. 我有一个实现ListFragment的“ ListFrag”类,在该类中,我有一个嵌套的静态DialogFragment类“ SortDialogFragment”,可通过选项菜单中的一个选项调用。 The purpose of the dialog is to provide the user with the set of options on how they want the list to be sorted. 对话框的目的是向用户提供有关他们希望列表如何排序的选项集。 I am able to display the dialog just fine and the user is able is to choose the option. 我能够很好地显示对话框,并且用户能够选择该选项。 However, I am not sure how I would refresh the listview without having access to listview's "notifyDataSetChanged" method from the DialogFragment class, once the user has made their choice. 但是,我不确定在用户做出选择后,如何从DialogFragment类访问listview的“ notifyDataSetChanged”方法时如何刷新listview。 I do know about communicating between two fragments through the underlying activity using an interface but I am not clear about how I would apply that technique in this particular case. 我确实知道使用接口通过基础活动在两个片段之间进行通信,但是我不清楚如何在这种特殊情况下应用该技术。 I am not sure where I would define my interface and who would implement that interface. 我不确定在哪里定义我的接口,以及谁将实现该接口。 Please bear with me since I am a newbie Android Developer and also to this site, in terms of posting questing. 由于我是新手Android开发人员,因此也请耐心等待,并且在发布查询方面也请访问此网站。 Below is my modified code, only including relevant code: 以下是我修改的代码,仅包含相关代码:

public class ListFrag extends SherlockListFragment {
    private ListFragListener myListener;
    private DatabaseAdapter database;
    private ArrayList<MyObject> listItems;
    private View layout;
    private Button confirmBtn;
    private Button cancelBtn;
    private boolean isDeleteActive;
    private boolean isExportActive;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        layout = inflater.inflate(R.layout.list_frag, container, false);    

        return layout;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        database = DatabaseAdapter.getInstance(getActivity());

        database.open();
        listItems = database.getMyObjects();
        database.close();

        setHasOptionsMenu(true);
        registerForContextMenu(this.getListView());

        final MyAdapter listAdapter = new MyAdapter(getActivity(), listItems);
        setListAdapter(listAdapter);

        confirmBtn = (Button) layout.findViewById(R.id.itemDelete);
        cancelBtn = (Button) layout.findViewById(R.id.itemCancel);

    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.listview_omenu, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        int itemId = item.getItemId();

        if (itemId == R.id.listOMItem3) {
            SortDialogFragment sortDialog = SortDialogFragment.newInstance(listItems);
            sortDialog.show(getFragmentManager(), "Sort Dialog");
        } 
        return true;
    }

    public static class SortDialogFragment extends SherlockDialogFragment {

        public static SortDialogFragment newInstance(ArrayList<MyObject> objectsToSort) {
            SortDialogFragment sortDialog = new SortDialogFragment();
            Bundle args = new Bundle();
            args.putParcelableArrayList("MyObjects to sort", objectsToSort);
            sortDialog.setArguments(args);
            return sortDialog;
        }

        @Override 
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

            final ArrayList<MyObjects> objectsToSort = getArguments().getParcelableArrayList("MyObjects to sort");


            builder.setTitle("Sort objects by...");
            builder.setSingleChoiceItems(R.array.sortDialogOptions, checkedItem, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub
                    if (which == 0) {
                        Collections.sort(objectsToSort, new ComparatorOne());
                    } else if (which == 1) {
                        Collections.sort(objectsToSort, new ComparatorTwo());
                    } else {
                        Collections.sort(objectsToSort, new ComparatorThree());
                    }

                    ?????????????????????????????????????
    // This is where I am trying to refresh the list before dismissing the dialog but I 
// I apparently do not have access to listview's adapter to call the "notifyDataSetChanged" method.


                    dialog.dismiss();
                }
            });

            builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            return builder.create();
        }
    }
}

First to refresh your listview you have to call notifyDataSetChanged on the adapter that you've set for your listview. 首先,刷新列表视图时,必须为列表视图设置的适配器上调用notifyDataSetChanged Start by changing final MyAdapter listAdapter to a field instead of a variable. 首先将final MyAdapter listAdapter更改为字段而不是变量。

Next step is to pass the adapter to the constructor of your dialog and store it in a field. 下一步是将适配器传递给对话框的构造函数,并将其存储在字段中。 The header of your SortDialogFragment should look like this: 您的SortDialogFragment的标题应如下所示:

 public static class SortDialogFragment extends SherlockDialogFragment {
    private MyAdapter mAdapter;
    public static SortDialogFragment newInstance(ArrayList<MyObject> objectsToSort, MyAdapter adapter) {
        SortDialogFragment sortDialog = new SortDialogFragment();
        sortDialog.setAdapter(adapter);
        Bundle args = new Bundle();
        args.putParcelableArrayList("MyObjects to sort", objectsToSort);
        sortDialog.setArguments(args);
        return sortDialog;
    }

    public void setAdapter (MyAdapter adapter){
         mAdapter = adapter;
    }
   (...)

Next pass the adapter when you create your SortDialogFragment. 在创建SortDialogFragment时,下一步传递适配器。 Should look like this: 应该看起来像这样:

SortDialogFragment sortDialog = SortDialogFragment.newInstance(listItems, listAdapter);

Finally you just have to call mAdapter.notifyDataSetChanged(); 最后,您只需要调用mAdapter.notifyDataSetChanged(); on ???????????????? ????????????????

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM