添加自己的侦听器列表

添加自己的侦听器列表

本文介绍了添加自己的侦听器列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试添加一个侦听器列表中,但没有我不明白为什么,如果你想看到的code的其余部分,请检查的

 公共无效onCreatebis(最终ResolveInfo resolveInfo){            的setContentView(R.layout.main);
            最终意图mainIntent =新意图(Intent.ACTION_MAIN,NULL);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
            最终的软件包管理系统PM = getApplicationContext()getPackageManager()。
            最终的ArrayList< ResolveInfo> listP =(ArrayList的&所述; ResolveInfo&GT)pm.queryIntentActivities(mainIntent,0);
            最终诠释trimLength =com.android长度()。
            ArrayList的<串GT; maliste =新的ArrayList<串GT;();
                        //循环每一个项目。
            对于(ResolveInfo信息:listP){
                //获取(完整,合格的)包名。
                字符串packag = info.activityInfo.applicationInfo.packageName;                //现在,随着子和修剪长度修剪。
                字符串修剪= packag.substring(trimLength);
                maliste.add(修剪);
            }
            ListView控件列表=(ListView控件)findViewById(R.id.list);
            monadaptateur适配器2 =新monadaptateur(这一点,maliste);            list.setOnItemClickListener(新OnItemClickListener(){                @覆盖
                公共无效onItemClick(适配器视图<>为arg0,ARG1观,诠释ARG2,
                        长ARG3){
                    Log.v(lalalala,LALALA);                }
                   });
            list.setAdapter(适配器2);
    }


解决方案

如果在ListView具有聚焦的项目则 onClickListener 将触发 onItemClickListener代替。设置项可以集中精力为false

  list.setItemsCanFocus(假);

看看这个thread.同时请注意这是你的解决方法。但是,更好的选择是设置物品的非聚焦的,并使用 OnItemClickListener ,或使它们可聚焦和使用 onClickListener 上意见

此外, onClickListener 不应该为ListView设置。相反,在 getView()方法每个ListView项

  @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
    // ...    view.setOnClickListener(新OnClickListener(){        @覆盖
        公共无效的onClick(视图v){
            //你的code
        }    });    返回视图。
}

i tried to add a listener to that list but nothing i don't understand why if you want to see the rest of the code please check on add your own listener to a list

public void onCreatebis(final ResolveInfo resolveInfo)  {

            setContentView(R.layout.main);
            final Intent mainIntent=new Intent(Intent.ACTION_MAIN,null);
            mainIntent.addCategory(Intent.CATEGORY_LAUNCHER) ;
            final PackageManager pm = getApplicationContext().getPackageManager();
            final ArrayList<ResolveInfo> listP= (ArrayList<ResolveInfo>)                    pm.queryIntentActivities( mainIntent, 0);
            final int trimLength = "com.android.".length();
            ArrayList<String> maliste = new ArrayList<String>();
                        // Loop over each item.
            for (ResolveInfo info : listP) {
                // Get the (full, qualified) package name.
                String packag = info.activityInfo.applicationInfo.packageName;

                // Now, trim it with substring and the trim length.
                String trimmed = packag.substring(trimLength);
                maliste.add(trimmed);
            }
            ListView list = (ListView)findViewById(R.id.list);
            monadaptateur adapter2 = new monadaptateur(this, maliste);

            list.setOnItemClickListener(new OnItemClickListener(){

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                        long arg3) {
                    Log.v("lalalala","lalala");

                }
                   });
            list.setAdapter(adapter2);
    }
解决方案

if the ListView has focusable items then onClickListener will fire instead of the onItemClickListener. Set items can focus to false

list.setItemsCanFocus(false);

have a look at this thread. Also note there are workarounds for this. But the better choice would be to set items non focusable and use OnItemClickListener, or make them focusable and use an onClickListener on the views

Also, the onClickListener should not be set for the listview. Instead for each listview item in the getView() method

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // ...

    view.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // your code
        }

    });

    return view;
}

这篇关于添加自己的侦听器列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:10