ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - ListView setOnItemClickListener in ListFragment not working

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android错误之ListView设置setOnItemClickListener失效

检查一下你的listView中item的view中有没有checkBox控件,如果有这个控件,事件会被子View捕获到,ListView无法捕获处理该事件,导致失效 解决方法如下: <Chec

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!!!

android android-fragments actionbarsherlock android-listfragment
|
  this question
asked Oct 11 '12 at 3:35 siddarthkaki 25 1 8

 | 

1 Answers
1

解决方法

You can also override a onListItemClick method that is inherited from the SherlockListFragment.

As follows:

推荐:Multiple choice listview in Android

Below is a simple yet effective solution of using checkbox in listviews. XML File <xml version="1.0" encoding="utf-8"><LinearLayout xmlns:android="

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

}

|
  this answer
answered Oct 11 '12 at 5:19 Marco RS 5,522 2 21 30      Hi Marco, thanks for the reply! It works just fine! –  siddarthkaki Oct 13 '12 at 3:24      This helped me out. Thanks. –  ernell Nov 21 '12 at 9:10

 | 

推荐:Android 中解决 ListView 等视图 setOnItemClickListener() 方法失效的问题

Java 部分1 list.setItemsCanFocus(false); XML 部分android:focusable=”false”android:focusableInTouchMode=”false”说明:如果一个 Lis


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。