ListView onItemClick启动另一个ListView - 带有片段

时间:2015-06-02 14:43:18

标签: android-fragments android-listview android-listfragment nested-lists child-fragment

启动时,我的应用程序启动一个片段,显示“项目”的列表视图。当我点击一个项目时,会启动一个新片段,显示项目的详细信息,包括另一个“任务”列表视图(每个项目对象可以包含许多任务)。

我无法为第二个listview初始化arrayadapter。 tasksAdapter = new TasksArrayAdapter(getActivity(),selectedProject.getTasks());

它不会编译,Android Studios提供以下原因:

incompatible types. Required: ProjectsArrayAdapter. Found: TasksArrayAdapter

在前一个片段上设置了ProjectsArrayAdapter,如下所示:

    //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();
        }
    });

为什么我无法创建这个新的数组适配器?

1 个答案:

答案 0 :(得分:0)

这是因为tasksAdapter被声明为ProjectsArrayAdapter而不是TasksArrayAdapter-复制/粘贴错误。

static TasksArrayAdapter tasksAdapter;