如何将更改的数据传回Fragment的列表?

时间:2016-01-09 16:58:48

标签: android

我有ListFragment个任意数据。当我点按某个项目时,它的数据会通过Bundle传递到另一个EditFragment,我会在其中检索并存储在EditText中进行编辑。如何传回已编辑的数据并对arraylist中的所选项目应用更改?同样通过Bundle?我是否应该同时访问ListFragment的列表视图?

EditFragment:

public class EditFragment extends Fragment {

    MenuItem save;
    MenuItem cancel;
    View view;
    EditText editText;
    int position;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        view = inflater.inflate(R.layout.edit_fragment_layout, container, false);
        editText = (EditText) view.findViewById(R.id.editText);
        Bundle b = getArguments();

        position = b.getInt("position");

        editText.setText(b.getString("name"));
        return view;
    }

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

        save = menu.findItem(R.id.save);
        cancel = menu.findItem(R.id.cancel);

        cancel.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                MyListFragment myListFragment = (MyListFragment) getFragmentManager().findFragmentByTag("MyListFragment");

                fragmentTransaction.replace(R.id.fragContainer, myListFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

                return true;
            }
        });

        save.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                MyListFragment myListFragment = (MyListFragment) getFragmentManager().findFragmentByTag("MyListFragment");
                //here we need to save changed data in our arraylist
                Bundle bundle = new Bundle();
                bundle.putInt("position", position);
                bundle.putString("name", editText.getText().toString());
                myListFragment.setArguments(bundle);
                //should I get my bundle data here?
                Bundle b = getArguments();

                //ArrayAdapter adapter = (ArrayAdapter) myListFragment.getListView().getAdapter();

                fragmentTransaction.replace(R.id.fragContainer, myListFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();

                return true;
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

创建EditFragment后,通过调用myEditFragment.setTargetFragment(this,0);将调用ListFragment设置为EditFragment的目标。

在EditFragment中,使用getTargetFragment()获取该片段对象,并调用片段的onActivityResult方法。

在列表片段中覆盖onActivityResult并从intent获取bundle以获取数据。

希望它有所帮助。