2

I am using import android.support.v4.app.ListFragment to create a ListFragment. Inside the fragment itself -- not the parent activity -- I want to implement the onItemClick method. Will someone please provide a simple example on how this might work?

public class MyListFragment extends ListFragment {

    Spannable[] stuff = {};//to be filled

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ArrayAdapter<Spannable> adapter = new ArrayAdapter<Spannable>(inflater.getContext(), android.R.layout.simple_list_item_1, stuff);
        setListAdapter(adapter);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//this is red underlined by eclipse
    }
}
Katedral Pillon
  • 14,534
  • 25
  • 99
  • 199
  • You should use an interface in your fragment then pass back the arguments inside onListItemClick so you know which item was clicked; you don't appear to have tried anything at all at this point – Eenvincible Apr 01 '14 at 18:15
  • @Eenvincible `you don't appear to have tried anything at all at this point`: You don't appear to have read my question otherwise you would have seen my class and the attempt at overriding `onItemClick`. Cheers! – Katedral Pillon Apr 01 '14 at 18:27
  • I guess what I meant was I would like to see any errors that you got but am glad it is solved now. Cheers and happy coding! – Eenvincible Apr 01 '14 at 18:39
  • @Eenvincible You mean like where my post says : `//this is red underlined by eclipse`. Just read better next time before making these cheap non-constructive comments that bring nothing to the table. I see a lot of people leaving the same cheap comments all over the place, you just happen to be the one to leave one on my question. Too bad I can't down vote comments. – Katedral Pillon Apr 01 '14 at 18:45
  • Why are you making a big deal out of this? If you were really careful in the first place, you wouldn't have used a wrong method in the wrong place and so if you think I didn't read your question, how then did I propose using onLiistItemClick method? Maybe you should read documentation before wasting people's times! NOTE: And just saying //this is red underlined by eclipse says nothing to be frank with you; give enough details, it is free! – Eenvincible Apr 01 '14 at 18:48
  • You see, you just assumed then accused me of down voting your question; just like you assumed that onItemClick was the right method to override. I will not allow myself to go that low because I can do better than just go around SO downvoting! – Eenvincible Apr 01 '14 at 19:05

1 Answers1

4

Have you done some research before asking that? Anyway, it can be done by overriding onListItemClick method, as following:

public class Test extends ListFragment {

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        //do the stuff

    }
}
nikis
  • 11,166
  • 2
  • 35
  • 45