Добавление элемента в расширяемый список из другого фрагмента - PullRequest
0 голосов
/ 23 марта 2019

Я пытаюсь добавить детали из popupMenu в одном фрагменте (ChestExerciseDetailsFragment), в расширяемое представление списка в другом фрагменте (TrainingFragment).Я полагаю, что мне нужно либо использовать Bundle, либо, возможно, использовать общие настройки.Но я не могу понять, как это сделать.

Может ли кто-нибудь помочь, как я это сделаю?

Это фрагмент, из которого я хочу получить данные

public class ChestExerciseDetailsFragment extends Fragment {

    Toolbar mToolbar;
    ImageView mImageView;
    TextView textView;
    Button btnAdd;

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

        mToolbar = view.findViewById(R.id.toolbar2);
        mImageView = view.findViewById(R.id.imageView2);
        textView = view.findViewById(R.id.textView2);
        btnAdd = view.findViewById(R.id.btnAdd);

        final Bundle bundle = getArguments();
        if(bundle != null){
            mToolbar.setTitle(bundle.getString("chestExerciseNames"));
            mImageView.setImageResource(bundle.getInt("chestExerciseGifs"));
            textView.setText(bundle.getString("chestExerciseDescription"));

        }

        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final PopupMenu popupMenu = new PopupMenu(getActivity(), btnAdd);
                popupMenu.getMenuInflater().inflate(R.menu.menu_week, popupMenu.getMenu());

                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {

                return true;
                }
             });
             popupMenu.show();
            }
        });
        return view;
    }
}

И фрагмент, в котором мне нужны данные:

public class TrainingFragment extends Fragment {

    private ExpandableListView listView;
    private ListviewAdapterWeek listAdapter;
    private List<String> exerciseList;
    private HashMap<String, List<String>> weekDays;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_training, container, false);

        listView = (ExpandableListView)view.findViewById(R.id.weekDays);
        initData();
        listAdapter = new ListviewAdapterWeek(getActivity(), exerciseList, weekDays);
        listView.setAdapter(listAdapter);

        return view;
    }

    private void initData() {

        exerciseList = new ArrayList<>();
        weekDays = new HashMap<>();

        //Headers. Days.
        exerciseList.add("Monday");
        exerciseList.add("Tuesday");
        exerciseList.add("Wednesday");
        exerciseList.add("Thursday");
        exerciseList.add("Friday");
        exerciseList.add("Saturday");
        exerciseList.add("Sunday");

        //Add items to the week days
        List<String> Monday = new ArrayList<>();
        Monday.add("This is where they go!");

        List<String> Tuesday = new ArrayList<>();
        Tuesday.add("This is where they go!!");

        List<String> Wednesday = new ArrayList<>();
        Wednesday.add("This is where they go!!");

        List<String> Thursday = new ArrayList<>();
        Thursday.add("This is where they go!!");

        List<String> Friday = new ArrayList<>();
        Friday.add("This is where they go!!");

        List<String> Saturday = new ArrayList<>();
        Saturday.add("This is where they go!!");

        List<String> Sunday = new ArrayList<>();
        Sunday.add("This is where they go!!");

        weekDays.put(exerciseList.get(0), Monday);
        weekDays.put(exerciseList.get(1), Tuesday);
        weekDays.put(exerciseList.get(2), Wednesday);
        weekDays.put(exerciseList.get(3), Thursday);
        weekDays.put(exerciseList.get(4), Friday);
        weekDays.put(exerciseList.get(5), Saturday);
        weekDays.put(exerciseList.get(6), Sunday);
    }    
}
...