如何从另一个片段访问ListFragment的arraylist?

时间:2016-01-10 22:05:16

标签: android android-fragments

已经困扰了以下问题几天了。在这里,我将ListFragment中某个项目的String和位置传递给另一个EditFragment,用户可以在其中编辑此String。

我使用以下方案进行沟通:

ListFragment --> Activity --> EditFragment. 

在EditFragment中,我将ListFragment中的点击数据从Bundle中取出并存储在EditText中,用户可以将其保存。如何将其保存回ListFragment?我相信我不能再次使用该包,因为当我尝试时,我得到一个错误,该片段已经处于活动状态。

这是我的EditFragment:

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 = getArguments();

        position = bundle.getInt("position");

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

    //SAVE button do save data:
    save.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {

            //Here I need to save data back in ListFragment

                return true;
            }
        });

3 个答案:

答案 0 :(得分:0)

我认为正确的解决方案是传递Intents,它应该可以正常工作(用于创建EditText片段而不是束,然后再传回数据)。

Intent intent = new Intent(this, EditTextActivity.class)
                .putExtra("position", position)
                .putExtra("name", name);
        startActivity(intent);

然后在片段中提取数据:

Intent intent = this.getActivity().getIntent();
int position = intent.getIntExtra("position");
String name = intent.getStringExtra("name");

答案 1 :(得分:0)

我真的不确定这是否有用,但你可以尝试一下。我还没有测试过。
您需要EditFragment中的回调,您应该尝试创建an interface来触发String的更新事件。

interface中创建EditFragment

public class EditFragment extends Fragment {
    private EditCallback editCallback;

    public interface EditCallback {
        void updateItem(int position, String textUpdated);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        editCallback = (EditCallback) context
    }

    ...
    @Override
    public void onDetach() {
        super.onDetach();
        editCallback = null;
    }
}

然后,当您想要从updateItem(int, String)更新项目时触发此方法ListFragment

save.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        // trigger the callback method
        if (editCallback != null) {
            editCallback.updateItem(position, editText.getText().toString());
        }
        return true;
    }
});

因此,当一个类实现这个interface时,它将调用updateItem()覆盖方法。这就是ListFragment应该使用此接口的方式:

public class ListFragment extends Fragment implements EditFragment.EditCallback {
    ...
    @Override
    public void updateItem(int position, String textUpdated) {
         // update the item's list by position,
         // call notifyDataSetChanged() on adapter
         // do some stuff...
    }
}

答案 2 :(得分:0)

在此处更新我的代码:

MainActivity:

numpy

ListFragment:

public class MainActivity extends AppCompatActivity implements MyListFragment.Communicator{

    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;
    FragmentTransaction fragmentTransaction2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MyListFragment myListFragment = new MyListFragment();
        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragContainer, myListFragment, "MyListFragment");

        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }

    @Override
    public void onMyListItemClick(String name, int position) {
        EditFragment editFragment = new EditFragment();

        Bundle args = new Bundle();
        args.putString("name", name);
        args.putInt("position", position);
        editFragment.setArguments(args);

        fragmentTransaction2 = getFragmentManager().beginTransaction();
        fragmentTransaction2.replace(R.id.fragContainer, editFragment);

        fragmentTransaction2.addToBackStack(null);
        fragmentTransaction2.commit();
    }
}

EditFragment:

public class MyListFragment extends ListFragment implements EditFragment.EditCallback {

    Communicator communicator;
    private MenuItem addItem;
    private MenuItem clearAll;

    ArrayList list1 = new ArrayList();
    String[] list2 = {"Sunday", "Monday",
            "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
    ArrayAdapter<String> adapter;
    int randomNum;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        communicator = (Communicator) getActivity();
        adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, list1);
        setListAdapter(adapter);
        setHasOptionsMenu(true);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

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

        addItem = menu.findItem(R.id.add_string);
        clearAll = menu.findItem(R.id.clear);

        addItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                Random rand = new Random();
                randomNum = rand.nextInt(list2.length);
                list1.add(list2[randomNum]);
                adapter.notifyDataSetChanged();
                return true;
            }
        });

        clearAll.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                list1.clear();
                adapter.notifyDataSetChanged();
                return true;
            }
        });
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        String name = (String) l.getItemAtPosition(position);
        communicator.onMyListItemClick(name, position);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void updateItem(int position, String textUpdated) {
        list1.set(position, textUpdated);
        adapter.notifyDataSetChanged();
    }

    public interface Communicator{
        void onMyListItemClick(String name, int position);
    }
}