android-fragmentsandroid-listviewandroid-listfragmentnested-listschild-fragment

ListView onItemClick launches another ListView - with Fragments


On startup, my app launches a fragment which displays a listView of 'projects'. When I click on a project, a new fragment is launched which displays the project's details, including another listView of 'tasks' (each project object can contain many tasks).

I am having trouble initializing the arrayadapter for the second listview. tasksAdapter = new TasksArrayAdapter(getActivity(),selectedProject.getTasks());

It wont compile, and Android Studios offers the following reason:

incompatible types. Required: ProjectsArrayAdapter. Found: TasksArrayAdapter

the ProjectsArrayAdapter was set up on the previous fragment like so:

    //initialize a custom ArrayAdapter to connect the object to the listView

    projectsAdapter = new ProjectsArrayAdapter(getActivity(), Projects.getInstance().getProjects());
    projectsList = (ListView) view.findViewById(R.id.listViewProjects);

    projectsList.setAdapter(projectsAdapter);

    //=========================================================================================
    //when you click on a project from the list
    //=========================================================================================

    //create onClickListener for listView items and launch project details fragment on click

    projectsList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            FragmentProject myProject = new FragmentProject();

            //grab the object you clicked on
            Bundle args = new Bundle();
            args.putInt("ARG_POSITION", position);
            myProject.setArguments(args);

            //launch project details fragment
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            transaction.add(R.id.main_layout, myProject);
            transaction.commit();
        }
    });

Why cant I create this new array adapter?


Solution

  • It was because tasksAdapter was declared as a ProjectsArrayAdapter instead of a TasksArrayAdapter- a copy/paste error.

    static TasksArrayAdapter tasksAdapter;