ListView onItemClick 启动另一个 ListView - 带有 Fragments
ListView onItemClick launches another ListView - with Fragments
启动时,我的应用会启动一个显示 'projects' 列表视图的片段。当我点击一个项目时,会启动一个显示项目详细信息的新片段,包括另一个 'tasks' 的 listView(每个项目对象可以包含许多任务)。
我在为第二个列表视图初始化数组适配器时遇到问题。
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();
}
});
为什么我不能创建这个新的阵列适配器?
这是因为 tasksAdapter 被声明为 ProjectsArrayAdapter 而不是 TasksArrayAdapter- copy/paste 错误。
static TasksArrayAdapter tasksAdapter;
启动时,我的应用会启动一个显示 'projects' 列表视图的片段。当我点击一个项目时,会启动一个显示项目详细信息的新片段,包括另一个 'tasks' 的 listView(每个项目对象可以包含许多任务)。
我在为第二个列表视图初始化数组适配器时遇到问题。
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();
}
});
为什么我不能创建这个新的阵列适配器?
这是因为 tasksAdapter 被声明为 ProjectsArrayAdapter 而不是 TasksArrayAdapter- copy/paste 错误。
static TasksArrayAdapter tasksAdapter;