将项目从另一个片段添加到expandablelistview

时间:2019-03-23 14:07:16

标签: java android expandablelistview popupmenu

我正在尝试将一个片段(ChestExerciseDetailsFragment)中的popupMenu的详细信息添加到另一个片段(TrainingFragment)中的expandableListview中。我想我要么需要使用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);
    }    
}

0 个答案:

没有答案