ListFragment中的ListView setOnItemClickListener不起作用

时间:2012-10-11 03:35:13

标签: android android-fragments actionbarsherlock android-listfragment

我正在开发一个使用ActionBar标签的应用程序,通过ListFragment显示选项列表。列表(和ListFragment)显示没有问题,但ListView的setOnItemClickListener似乎不起作用,因为单击列表中的项目时没有任何反应。这是ListFragment类的代码:

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

我认为它不起作用,因为该类返回视图对象,因此FragmentActivity无法运行侦听器代码,所以有人知道如何使其工作吗?顺便说一句,我正在使用ActionBarSherlock。在此先感谢!!!

1 个答案:

答案 0 :(得分:6)

您还可以覆盖从SherlockListFragment继承的onListItemClick方法。

如下:

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
      super.onListItemClick(l, v, position, 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;
                    }

}