androidfragmentonitemclicklisteneronitemclick

onListItemClick in a fragment


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

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

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

}

How can I 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).


Solution

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

    Or you can just move the code from your onListItemClick() method into onItemClick()

    EDIT:

    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.