ListView setOnItemClickListener in ListFragment not working

Posted by Siddarth Kaki on Stack Overflow See other posts from Stack Overflow or by Siddarth Kaki
Published on 2012-10-11T03:35:12Z Indexed on 2012/10/11 3:36 UTC
Read the original article Hit count: 211

I am developing an app that uses ActionBar tabs to display a list of options through ListFragment. The list (and ListFragment) display without a problem, but the ListView's setOnItemClickListener doesn't seems to work, as nothing happens when an item in the list is clicked. Here's the code for the ListFragment class:

package XXX.XXX;

public class AboutFrag extends SherlockListFragment
{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState)
    {
            View view = inflater.inflate(R.layout.aboutfrag, container, false);

            ListView lv = (ListView) view.findViewById(android.R.id.list);

            String[] items = new String[] {"About 1", "About 2", "About 3"};

            lv.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.list_item, items));

            lv.setOnItemClickListener(new OnItemClickListener() {
                public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                    switch (position)
                    {
                        case 0:
                          Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com"));
                          startActivityForResult(browserIntent, 0);
                          break;

                        case 1:
                          Intent browserIntent2 = new Intent(Intent.ACTION_VIEW, Uri.parse("http://wikipedia.org"));
                            startActivityForResult(browserIntent2, 0);
                            break;

                        case 2:
                          Intent browserIntent3 = new Intent(Intent.ACTION_VIEW, Uri.parse("http:/android.com");
                            startActivityForResult(browserIntent3, 0);
                            break;
                    }           
                }
              });

            return view;
    }      
}

I'm assuming it does not work because the class returns the view object, so the FragmentActivity can't run the listener code, so does anyone know how to make this work? By the way, I am using ActionBarSherlock. Thanks in advance!!!

© Stack Overflow or respective owner

Related posts about android

Related posts about android-fragments