Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am developing a book application. Main menu contains chapter numbers which are 13.
Each chapter contains different number of exercise( 7-10), and each exercise contains different number (10-15) of images (scanned images).
Now am using two fragments,
fragmentA for showing exercises for each chapter, I have created arraylists of exercises and using listview to load them into fragmentA. which is working perfect.

fragmentB is for showing the images (scanned copies).

Now how can I manage clicks on listviews which are loaded into fragmentA?

By using interface to communicate between fragments?

Please show me some examples of this situation?

This is how am passing data to fragmentA.

Java
@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit1);
                break;
            case R.id.button2:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit2);
                break;
            case R.id.button3:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA,  unit3);
                break;
            case R.id.button4:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit4);
                break;
            case R.id.button5:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit5);
                break;
            case R.id.button6:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA,  unit6);
                break;
            case R.id.button7:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit7);
                break;
            case R.id.button8:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit8);
                break;
            case R.id.button9:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA,  unit9);
                break;
            case R.id.button10:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit10);
                break;
            case R.id.button11:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA, unit11);
                break;
            case R.id.button12:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA,  unit12);
                break;
            case R.id.button13:
                bundle = new Bundle();
                bundle.putStringArrayList(LISTVIEW_DATA,  unit13);
                break;
            default:
                break;
        }

        setListFragment(bundle);
    }
    private void setListFragment(Bundle arguments) {
        Fragment fragment = new ExFragment();

        if (arguments != null) {
            fragment.setArguments(arguments);
        }

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.container, fragment);
        ft.addToBackStack("");
        ft.commit();
    }


This is onitemclick for in fragmentA.

XML
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view;



But if i apply something here it applies to all the chapters i.e unit1, unit2 (these are arraylists of chapters) , now what I want to ask is how i can apply actions to all the exercises individually.

If u dont understand anything in question please do ask.
Thanks
Posted
Comments
Maciej Los 9-Jan-16 5:32am    
What is unit1, unit2, unitN inside onClick event?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900