0

我试图在该列表中添加一个侦听器,但我不明白为什么如果您想查看其余代码,请检查将您自己的侦听器添加到列表中

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

1 回答 1

2

如果 ListView 具有可聚焦的项目,onClickListener则将触发而不是onItemClickListener. 设置项目可以聚焦为假

list.setItemsCanFocus(false);

看看这个线程。另请注意,有解决方法。但更好的选择是将项目设置为不可聚焦并使用OnItemClickListener,或者使它们可聚焦并onClickListener在视图上使用

此外,onClickListener不应为列表视图设置 。代替getView()方法中的每个列表视图项

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

    view.setOnClickListener(new OnClickListener() {

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

    });

    return view;
}
于 2013-03-13T09:24:34.460 回答