onListItemClick在片段 [英] onListItemClick in a fragment

查看:244
本文介绍了onListItemClick在片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个迫切的问题。我想AP preciate,如果有人可以帮助我。

I have an urgent problem. I would appreciate it if someone could help me.

我有延伸活动主要活动。它采用抽屉式导航Android这样的样本。在滑动菜单我有章节列表。当点击了那一段的教训列表显示在主要活动中的内容片段。

I have a Main Activity which extends Activity. It uses drawer navigation like in android sample. On the sliding menu I have a list of chapters. When clicked a list of lessons of that chapter is shown in content fragment inside the main activity.

因此​​,在主要活动我有一个扩展片段的类。设置该片段的内容具有这样的方法:

so in the main activity I have a class that extends Fragment. to set the content of the fragment it has a method like this:

    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方式:

and this is the refreshDisplay method that populates the listView with a costume adapter:

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()但随后得到的是点击的项目(章)的位置滑动菜单不是项目(课)列表内片段(我的内容片段)。

Now, How can I use a onListItemClick to start a new Activity for the detail of the clicked item? I have the method below. But I don't know where to put it. when I put in the Fragment after call to the refreshDisplay() but then it gets the position of the item (chapter) that is clicked is the sliding menu not the the item (lesson) in the list inside the fragment (my content fragment).

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);

}

任何人都可以请帮助我了解如何解决这一问题?
我想的是,我有一个包含章节列表的滑动菜单。当章点击与该章教训显示(全集至今)。然后点击一个教训时,该课的细节一个新的活动打开。 (这是我的问题)

Can anyone please help me understand how to fix this? what I want is that I have a sliding menu containing a list of chapters. when a chapter is clicked the lessons related to that chapter is shown (Works so far). And then when a lesson is clicked, a new activity for the detail of that lesson be opened. (this is my problem)

推荐答案

在你的 refreshDisplay()方法,一个监听器添加到这样的列表视图:

In your refreshDisplay() method, add a listener to the list view like this:

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()方法code到 onItemClick()

编辑:

我添加了如何从适配器中点击对象的例子。为了使它工作,适配器必须声明为成员变量。替换 MainActivity 与您的活动名称。

I added an example of how to get the clicked Lesson object from the adapter. To make it work, the adapter must be declared as a member variable. Replace MainActivity with the name of your activity.

这篇关于onListItemClick在片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆