onListItemClick in afragment

问题描述 投票:-1回答:1

我有一个扩展活动的主要活动。它使用抽屉导航,就像在android示例中。在滑动菜单上,我有一个章节列表。单击后,该章节的课程列表将显示在主要活动的内容片段中。

所以在主要活动中,我有一个扩展Fragment的类。要设置片段的内容,它具有如下方法:

    public static class contentFragment extends Fragment {
    public static final String ARG_CATEGORY_NUMBER = "category_number";

    public contentFragment() {
        // Empty constructor required for fragment subclasses
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {


            View rootView = inflater.inflate(R.layout.fragment_content, container, false);
            int i = getArguments().getInt(ARG_CATEGORY_NUMBER);
            String category = getResources().getStringArray(R.array.category)[i];
            ((MainActivity) this.getActivity()).refreshDisplay(this.getActivity(),rootView, category);
            getActivity().setTitle(category);
            return rootView;
        }
}

这是使用服装适配器填充listView的refreshDisplay方法:

public void refreshDisplay(Context context, View view, String category) {

    List<Lesson> lessonByCategory = datasource.findByCategory(category);
    ListView lv = (ListView) view.findViewById(R.id.listView);
    ArrayAdapter<Lesson> adapter = new LessonListAdapter(context, lessonByCategory);
    lv.setAdapter(adapter);
    }

现在,如何使用onListItemClick来为单击项的详细信息启动新的活动?我有下面的方法。但是我不知道该放在哪里。当我在调用refreshDisplay()之后将片段放在片段中,但随后它获得被单击的项目(章节)的位置是滑动菜单而不是片段内部列表中的项目(课程)的位置(我的内容片段)。

protected void onListItemClick(ListView l, View v, int position, long id) {
    Log.i(LOGTAG, "onListItemClick call shod");

    Lesson lesson = lessons.get(position);

    Intent intent = new Intent(this, LessonDetailActivity.class);

    intent.putExtra(".model.Lesson", lesson);
    intent.putExtra("isStared", isStared);

    startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);

}

我该如何解决?我想要的是我有一个包含章节列表的滑动菜单。单击某一章后,将显示与该章相关的课程(到目前为止有效)。然后,当单击课程时,将打开该课程详细信息的新活动(这是我的问题)。

android fragment onitemclicklistener onitemclick
1个回答
0
投票

在您的refreshDisplay()方法中,将侦听器添加到列表视图中,如下所示:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick (AdapterView<?> parent, View view, int position, long id) {
        Log.i(LOGTAG, "onListItemClick call shod");

        // get the Lesson object for the clicked row
        Lesson lesson = m_adapter.getItem(position);

        // use this if your `refreshDisplay()` method is in your activity
        Intent intent = new Intent(MainActivity.this, LessonDetailActivity.class);

        // use this if you `refreshDisplay()` method is in your fragment
        Intent intent = new Intent(getActivity(), LessonDetailActivity.class);

        intent.putExtra(".model.Lesson", lesson);
        intent.putExtra("isStared", isStared);

        startActivityForResult(intent, LESSON_DETAIL_ACTIVITY);
    }
}

或者您也可以将代码从onListItemClick()方法移至onItemClick()

编辑:

我添加了一个如何从适配器获取被单击的Lesson对象的示例。为了使其工作,适配器必须声明为成员变量。用您的活动名称替换MainActivity

© www.soinside.com 2019 - 2024. All rights reserved.