简体   繁体   English

将您自己的侦听器添加到列表中

[英]add your own listener to a list

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 . 如果ListView的具有可聚焦的项目,然后onClickListener会火,而不是onItemClickListener Set items can focus to false 设置项可以集中于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 但是,更好的选择是设置项非可聚焦和使用OnItemClickListener ,或使它们可聚焦和使用onClickListener的意见

Also, the onClickListener should not be set for the listview. 此外, onClickListener不应该为ListView设置。 Instead for each listview item in the getView() method 而是为getView()方法中的每个listview项

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

    view.setOnClickListener(new OnClickListener() {

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

    });

    return view;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM